SumanP89 SumanP89 - 1 year ago 78
Javascript Question

call multiple ajax call with Jquery deferred When and then(3 parallel calls and then on complete one more ajax call)

My requirement is to call 3 ajax call in parallel then only on complete of the 3 calls , I am requesting one more Ajax call. So for i am able to achieve this by the following this article by Chris from CSS-trick

$.get("url1"), //goes in parallel
$.get("url2"), //goes in parallel
$.get("url3") //goes in parallel
$.get("url4") // fires only on completion of 3 calls above.

But my code stuructre is like

//this gives 3 ajax calls from here
//this shoould be only called after 3 calls been successfull

This way it does not wait for the
to wait for the
method to finish and all calls(4 of them) goes in parallel/async.

How i should achieve this?

Answer Source

The example below shows how to use sequential calls with functions.

Rather than: when(singleCall()), do it like when(singleCall); without parantheses. Or, it will execute the function before the promise resolves.

// just open up the network tab in your browser and see that
// first 3 urls will go in parallel in threeCalls,
// and then the last url will go in singleCall

function threeCalls() {
  console.log("calling urls in parallel...");
  return [ 

function singleCall(results) {
  console.log("single call is here");
  return $.get("");

$.when.apply($, threeCalls())
<script src=""></script>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download