Last active
June 8, 2020 03:41
-
-
Save peisenmann/41488a45364974705cd6 to your computer and use it in GitHub Desktop.
Javascript Promises - allComplete() : Wait for all promises to complete. No short-circuit on rejection.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// TEST | |
let p1 = new Promise((resolve, reject) => resolve('good one')); | |
let p2 = new Promise((resolve, reject) => { | |
setTimeout(() => { | |
reject('bad one'); | |
}, 3000); | |
}); | |
let p3 = new Promise((resolve, reject) => resolve('good one')); | |
allComplete([p1, p2, p3, 'not-a-promise']).then(result => { | |
console.log('all completed ', result); | |
}); | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* allComplete - Return a promise to wait for all promises to complete. No short-circuit on rejection. | |
* | |
* This version uses ES5 and npm install promise for promises. | |
* | |
* @param promises {Array<Promise|*>} Expected usage is an array of promises, but the array can contain non-promises as well. | |
* @return {Promise<Array<*>>} A promise that only resolves once all promises from the arguments have resolved. | |
*/ | |
function allComplete(promises) { | |
return new Promise(function(resolve) { | |
var retVals = new Array(promises.length); | |
var states = new Array(promises.length); | |
for (var i = 0; i < promises.length; ++i) { retVals[i] = states[i] = false; } | |
var f = function(i) { | |
return function (res) { | |
retVals[i] = res; | |
states[i] = true; | |
if (states.every(function (s) {return s;})) { | |
resolve(retVals); | |
} | |
}; | |
}; | |
promises.forEach(function(p, i) { | |
Promise.resolve(p).then(f(i), f(i)); | |
}); | |
}); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* allComplete - Return a promise to wait for all promises to complete. No short-circuit on rejection. | |
* Demo: http://plnkr.co/edit/TZOCZVuk14Gjw9MrfCbw | |
* | |
* @param promises {Array<Promise|*>} Expected usage is an array of promises, but the array can contain non-promises as well. | |
* @return {Promise<Array<*>>} A promise that only resolves once all promises from the arguments have resolved. | |
*/ | |
function allComplete(promises) { | |
"use strict"; | |
return new Promise(resolve => { | |
let retVals = Array(promises.length).fill(); | |
let states = Array(promises.length).fill(); | |
let f = i => res => { | |
retVals[i] = res; | |
states[i] = true; | |
if (states.every(s => s)) { | |
resolve(retVals); | |
} | |
}; | |
promises.forEach((p, i) => { | |
Promise.resolve(p).then(f(i), f(i)); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This saved me, holy moly.