Question: I'm having some trouble. I'm trying to execute my ajax function 1 by 1, not all at the same time. I'm using `promise` but I have no more idea on how to achieve it. Here is my code : 

```
function run_action(action){
    if(action == "login"){
        return $.ajax({
            url: "login.php",
            type: "post",
            data: {password: password},
            beforeSend: function() {
                console.log('beforeSend login');
            },
            success: function (response) {
                console.log('Success Login');
            },
            error: function (request, error) {
                console.log('Error Login');
            },
        })
    }
    if(action == "register"){
        return $.ajax({
            url: "register.php",
            type: "post",
            data: {password: password},
            beforeSend: function() {
                console.log('beforeSend register');
            },
            success: function (response) {
                console.log('Success Register');
            },
            error: function (request, error) {
                console.log('Error Register');
            },
        })
    }
}

var actions = ['register', 'login'];
services.forEach(checkActions);
function checkActions(item, index) {
    if (document.getElementById(item).checked) {
        var promise = run_action(item);
        promise.success(function (data) {
            console.log('Run after');
        });
        console.log('Run first')
    }
}

```

In this case `login` and `register` are both launched at the same time, `login` doesn't wait for `register` to finish so he can start processing.

Answer: 
In case you can't properly wait for `checkActions` from the outside, you could maintain a task queue for that:

```
 let queue = Promise.resolve();

 function checkActions(item, index) {
    queue = queue
     .then(() => run_action(item))
     .then(() => {
       console.log("Next item was processed", item);
       // Your code here
     });
   // Synchronous code here - This won't execute in order!
 }

```
First- its not a good practice to trust a order-based function (AKA - run them by the array order), run your functions according to logic. 

For example: if the first function was failed - you dont want to run the next functions in the array.

If you consist to run the functions in array - you can use an async 

```
async function runActions( actionsList ) {
  for(const action of actionsList) {
    await run_action( action );
  }
};

```

In general - we use the [`then`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) method to run anther function when specific promise is done. Like so:

```
promise.then( onSuccess => {
   // try to log in
}, onFail => {
   // tell the user the signup was failed
});

```

---

BTW - you can use the native [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) instade of jQuery ajax, and get simple to use, promise-based way to communicate with your sever.

Like so:

```
fetch("login.php" , {
  method: 'POST', // or 'PUT'
  body: {password: password}, // data can be `string` or {object}!
  headers:{
    'Content-Type': 'application/json'
  }
}).then( ... )

```