dojo/promise/all

Authors: Mark Wubben
since:V1.8

dojo/promise/all is a function that takes multiple promises and returns a new promise that is fulfilled when all promises have been resolved or one has been rejected.

Introduction

dojo/promise/all replaces dojo/DeferredList and provides a mechanism to manage multiple asynchronous processes by essentially combining the results of several promises into a single promise. An example usage would be several different XHR requests to several different back end services to get some results and then to do something when all the back end services have responded. This would increase the latency of the application because you would only wait for the longest service to respond.

Usage

Usage is straight forward, in that dojo/promise/all is a single argument that is either an Object or an Array. The results are either returned as an Object that uses the same keys as supplied in the argument or an Array in the same order that the supplied one:

require(["dojo/promise/all"], function(all){

  all([promise1, promise2]).then(function(results){
    // results will be an Array
  });

  // -- or --

  all({
    promise1: promise1,
    promise2: promise2
  }).then(function(results){
    // results will be an Object using the keys "promise1" and "promise2"
  });

});

As mentioned above, dojo/promise/all takes a single argument:

Argument Type Description
objectOrArray Object/Array? The object or array of promises to use

Examples

This example demonstrates simulating 3 service requests and then performing the handling of the results in one block of code.

require(["dojo/promise/all", "dojo/Deferred", "dojo/dom", "dojo/on", "dojo/json", "dojo/domReady!"],
function(all, Deferred, dom, on, JSON){

  function googleRequest(){
    var deferred = new Deferred();
    setTimeout(function(){
      deferred.resolve("foo");
    }, 500);
    return deferred.promise;
  }

  function bingRequest(){
    var deferred = new Deferred();
    setTimeout(function(){
      deferred.resolve("bar");
    }, 750);
    return deferred.promise;
  }

  function baiduRequest(){
    var deferred = new Deferred();
    setTimeout(function(){
      deferred.resolve("baz");
    }, 1000);
    return deferred.promise;
  }

  on(dom.byId("startButton"), "click", function(){
    dom.byId("output").innerHTML = "Running...";
    all([googleRequest(), bingRequest(), baiduRequest()]).then(function(results){
      dom.byId("output").innerHTML = JSON.stringify(results);
    });
  });

});
<h1>Output:</h1>
<pre id="output"></pre>
<button type="button" id="startButton">Start</button>

This example is essentially the same as above, but passes an Object as a parameter to dojo/promise/all.

require(["dojo/promise/all", "dojo/Deferred", "dojo/dom", "dojo/on", "dojo/json", "dojo/domReady!"],
function(all, Deferred, dom, on, JSON){

  function googleRequest(){
    var deferred = new Deferred();
    setTimeout(function(){
      deferred.resolve("foo");
    }, 500);
    return deferred.promise;
  }

  function bingRequest(){
    var deferred = new Deferred();
    setTimeout(function(){
      deferred.resolve("bar");
    }, 750);
    return deferred.promise;
  }

  function baiduRequest(){
    var deferred = new Deferred();
    setTimeout(function(){
      deferred.resolve("baz");
    }, 1000);
    return deferred.promise;
  }

  on(dom.byId("startButton"), "click", function(){
    dom.byId("output").innerHTML = "Running...";
    all({
      google: googleRequest(),
      bing: bingRequest(),
      baidu: baiduRequest()
    }).then(function(results){
      dom.byId("output").innerHTML = JSON.stringify(results);
    });
  });

});
<h1>Output:</h1>
<pre id="output"></pre>
<button type="button" id="startButton">Start</button>

See also

  • dojo/promise - The rest of the Dojo Promise API
  • dojo/promise/first - Like dojo/promise/all except gets fulfilled when the first promise gets fulfilled.
  • dojo/Deferred - The base class for managing asynchronous thread communication in Dojo.