Skip to content
Advertisement

Best way to chain jquery ajax calls which pass information

I am making several asynchronous ajax calls which must be executed in a specific order and which must pass information to each other. Here is a MWE of my current approach. Even with three API calls it is a bit of a nightmare. With 5 it’s impossible to line up the error workflow or to move functions around (also I run out of screen space). Is there a better approach to achieve this same outcome?

JavaScript

Related but not so clear questions:

  1. Chaining multiple jQuery ajax requests
  2. jquery how to use multiple ajax calls one after the end of the other

Advertisement

Answer

$.ajax returns a Thenable, which means you can await it inside an async function, which is easier to manage than nested callbacks:

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement