Skip to content
Advertisement

Jquery async/await ajax call

I’m currently using 3 ajax call methods (3 of them are executing back-to-back). I must have a time delay in between the second ajax call and the third one. If I add “async:false” in the second ajax, everything works like a charm. However, I found out that this is really a terrible practice and shouldn’t be using. So I decided to try an async/await. This is my first time, so have no luck. I would be really grateful if you guys could add some explanations so I can learn. Thank you so much.

JavaScript

and then I’m calling InsertAssignments(data) somewhere.

Advertisement

Answer

  1. Async / await requires functions to return a promise.
  2. jQuery $.ajax() can be used in two ways: with callbacks and promises. Your code is using a callback, not a promise.

Wrap $.ajax() in a function like this:

JavaScript

Then use await doAjax() whenever you are making an ajax call.

JavaScript
Advertisement