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!
 }

```
Currently your code runs through the forEach loop with each action and invokes checkActions with that action, thus firing the request. Array.prototype.forEach executes synchronously (without any kind of check to the promises returned by $.ajax). The following would wait for 'register' to finish before firing 'login':

```
function checkActions(item) {
  if (document.getElementById(item).checked) {
    return run_action(item);
  }
}

checkActions('register')
  .then(data => {
    return checkActions('login');
  });

```

I'm not super familiar with jQuery's promise structure, so I used .then, but I believe you can use .success without issue as well.

Unrelated comment, your run\_actions function should really be split into two functions (login and register) since they are completely unrelated aside from the fact that they are making requests.