Skip to content

Instantly share code, notes, and snippets.

@seamusleahy
Last active September 2, 2016 03:48
Show Gist options
  • Save seamusleahy/8fc0ed47746fba7ec0d6e11b1742f60f to your computer and use it in GitHub Desktop.
Save seamusleahy/8fc0ed47746fba7ec0d6e11b1742f60f to your computer and use it in GitHub Desktop.
How to deal with multiple promises with native code and several popular libraries. https://seamusleahy.com/how-to-resolve-multiple-promises/
module.controller('MyCtrl', function($scope, $q) {
const promise1 = somethingThatReturnsAPromise();
const promise2 = somethingElseThatReturnsAPromise();
const promise3 = yetAnotherCallThatReturnsAPromise();
// We create a wrapper promise for all the them using $q.all which follows Promise.all of taking an array of promises
const consolidatedPromise = $q.all([promise1, promise2, promise3]);
consolidatedPromise.then(function (promiseValues) {
console.log('Promise 1:', promiseValues[0]); // The value from `promise1.then(function(value){})`
console.log('Promise 2:', promiseValues[1]); // The value from `promise2.then(function(value){})`
console.log('Promise 3:', promiseValues[2]); // The value from `promise3.then(function(value){})`
});
});
// We have three promises we need to wait all to resolve before the next step
const promise1 = somethingThatReturnsAPromise();
const promise2 = somethingElseThatReturnsAPromise();
const promise3 = yetAnotherCallThatReturnsAPromise();
// We create a wrapper promise for all of them
const consolidatedPromise = Bluebird.all([promise1, promise2, promise3]);
consolidatedPromise.then(function(promiseValues) {
// promisesValues is an array of the values from all the promises
console.log('Promise 1:', promiseValues[0]); // The value from `promise1.then(function(value){})`
console.log('Promise 2:', promiseValues[1]); // The value from `promise2.then(function(value){})`
console.log('Promise 3:', promiseValues[2]); // The value from `promise3.then(function(value){})`
});
require(["dojo/promise/all", "dojo/Deferred"], function(all, Deferred) {
const promise1 = somethingThatReturnsAPromise();
const promise2 = somethingElseThatReturnsAPromise();
const promise3 = yetAnotherCallThatReturnsAPromise();
// We create a wrapper promise for all the them using dojo/promise/all which follows Promise.all of taking an array of promises
const consolidatedPromise = all([promise1, promise2, promise3]);
consolidatedPromise.then(function (promiseValues) {
// promisesValues is an array of the values from all the promises
console.log('Promise 1:', promiseValues[0]); // The value from `promise1.then(function(value){})`
console.log('Promise 2:', promiseValues[1]); // The value from `promise2.then(function(value){})`
console.log('Promise 3:', promiseValues[2]); // The value from `promise3.then(function(value){})`
});
});
// We have three promises we need to wait all to resolve before the next step
const promise1 = somethingThatReturnsAPromise();
const promise2 = somethingElseThatReturnsAPromise();
const promise3 = yetAnotherCallThatReturnsAPromise();
// We create a wrapper promise for all the them - notice you pass them each as arguments instead of passing an array
const consolidatedPromise = $.when(promise1, promise2, promise3);
consolidatedPromise.then(function (value1, value2, value3) {
// Each value is a parameter instead of being passed in as an array of values
console.log('Promise 1:', promiseValues[0]); // The value from `promise1.then(function(value){})`
console.log('Promise 2:', promiseValues[1]); // The value from `promise2.then(function(value){})`
console.log('Promise 3:', promiseValues[2]); // The value from `promise3.then(function(value){})`
});
// We have three promises we need to wait all to resolve before the next step
const promise1 = somethingThatReturnsAPromise();
const promise2 = somethingElseThatReturnsAPromise();
const promise3 = yetAnotherCallThatReturnsAPromise();
// We create a wrapper promise for all of them
const consolidatedPromise = Promise.all([promise1, promise2, promise3]);
consolidatedPromise.then(function(promiseValues) {
// promisesValues is an array of the values from all the promises
console.log('Promise 1:', promiseValues[0]); // The value from `promise1.then(function(value){})`
console.log('Promise 2:', promiseValues[1]); // The value from `promise2.then(function(value){})`
console.log('Promise 3:', promiseValues[2]); // The value from `promise3.then(function(value){})`
});
// We have three promises we need to wait all to resolve before the next step
const promise1 = somethingThatReturnsAPromise();
const promise2 = somethingElseThatReturnsAPromise();
const promise3 = yetAnotherCallThatReturnsAPromise();
// We create a wrapper promise for all of them
const consolidatedPromise = Q.all([promise1, promise2, promise3]);
consolidatedPromise.then(function(promiseValues) {
// promisesValues is an array of the values from all the promises
console.log('Promise 1:', promiseValues[0]); // The value from `promise1.then(function(value){})`
console.log('Promise 2:', promiseValues[1]); // The value from `promise2.then(function(value){})`
console.log('Promise 3:', promiseValues[2]); // The value from `promise3.then(function(value){})`
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment