0

I'm making sequential AJAX requests for my websites. There are 2 POST requests. The second request should be processed after first request is done. My code is as below:

$.ajax({
    type: 'POST',
    url: '/backend/edge/enableNewAgent/',
    async: false,
    success: function () {
        console.log("First Process Done");
    }
});
$.ajax({
    type: 'POST',
    url: '/backend/edge/deleteOldAgent/',
    async: false,
    success: function () {
        console.log("Second Process Done");
    }
});

The second process is done after first process, but the console logging is executed after second process done, not after first process done. I want the console.log is executed soon after first process done, then continue executing the second process. Can someone help ?

7
  • oops, I read "asynchronous" sorry - anything in the developer tools console to show what weird thing may be going on? .... note: you know the first A in AJAX stands for Asynchronous, so async:false is kind of an oxymoron :p Commented Oct 16, 2018 at 10:28
  • 1
    Never use async: false. It is the cause of the problem as the thread to update the console is blocked by the second AJAX request, so both items appear at the same time, after both requests have completed. To fix this use the callback pattern properly by making the second AJAX call within the success handler function of the first. Commented Oct 16, 2018 at 10:29
  • In Chrome, at least, console logging is not synchronous. By using async: false you don't yield control back to the event loop so the console.log lines get queued up. Commented Oct 16, 2018 at 10:29
  • in fact, not console.log, but like changing progressbar length from 10% to 20% @Alnitak Commented Oct 16, 2018 at 10:42
  • jsbin.com/kovisedoqu/1/edit?js,console — I can't reproduce the problem. Commented Oct 16, 2018 at 10:43

3 Answers 3

1

If you want to write synchronous "looking" code and avoid synchronous XMLHttpRequest - you can use async/await

async function doAjax() {
    await $.ajax({
        type: 'POST',
        url: '/backend/edge/enableNewAgent/',
        success: function() {
            console.log("First Process Done");
        }
    });
    await $.ajax({
        type: 'POST',
        url: '/backend/edge/deleteOldAgent/',
        success: function() {
            console.log("Second Process Done");
        }
    });
}

actually, it's better done like

async function doAjax() {
    await $.ajax({
        type: 'POST',
        url: '/backend/edge/enableNewAgent/'
    });
    console.log("First Process Done");
    await $.ajax({
        type: 'POST',
        url: '/backend/edge/deleteOldAgent/'
    });
    console.log("Second Process Done");
}

Note, it HAS to be done inside a function (doesn't have to be a separate function like this, just put that there to enforce the idea ... await is only inside async function)

Sign up to request clarification or add additional context in comments.

3 Comments

Sure, but if IE support is required this solution is smoked.
@EricHerlitz - have you heard of babel?
Yea, it's wild.
1

Using async: false means that you never yield to the event loop, and the console.log lines get queued up (as do all other display updates).

My approach would be this:

function enableNewAgent() {
    return $.post('/backend/edge/enableNewAgent/',
        () => console.log('enableNewAgent Done')
    );
}

function deleteOldAgent() {
    return $.post('/backend/edge/deleteOldAgent/',
        () => console.log('deleteOldAgent Done')
    );
}

enableNewAgent().then(deleteOldAgent);

If you require further operations, add them to the .then chain:

enableNewAgent().then(deleteOldAgent).then(nextOperation);

Comments

0

You can try

$.ajax({
  type: 'POST',
  url: '/backend/edge/enableNewAgent/',
  success: function() {
    console.log("First Process Done");
  }
}).then(function(){
      $.ajax({
      type: 'POST',
      url: '/backend/edge/deleteOldAgent/',
      success: function() {
        console.log("Second Process Done");
      }
    });

3 Comments

this probably won't fix the problem with the OP's console.log sequencing, but if you were to make it async: true instead (which is of course the right thing to do anyway) it would all work.
Still not wotking on >= 3rd ajax post :( stuck @Alnitak
@WilliamsPerdana there's a better than nested callbacks - see my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.