Skip to content

Instantly share code, notes, and snippets.

@indexzero
Created April 17, 2019 06:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save indexzero/131d053c958e49b87ad3f179e76a0d33 to your computer and use it in GitHub Desktop.
Save indexzero/131d053c958e49b87ad3f179e76a0d33 to your computer and use it in GitHub Desktop.
Promise.all proof by doing
Start | 1 | foo
Start | 2 | bar
Start | 3 | bazz
End | 2 | bar
End | 3 | bazz
End | 1 | foo
[ 'foo', 'bar', 'bazz' ]
function waitAndLog({ num, str, timeout }) {
return new Promise(function (resolve, reject) {
console.log(`Start | ${num} | ${str}`);
setTimeout(() => {
console.log(`End | ${num} | ${str}`);
resolve(str);
}, timeout);
});
}
var promise1 = waitAndLog({
num: 1,
str: 'foo',
timeout: 1000
})
var promise2 = waitAndLog({
num: 2,
str: 'bar',
timeout: 100
})
var promise3 = waitAndLog({
num: 3,
str: 'bazz',
timeout: 500
})
Promise.all([promise1, promise2, promise3]).then(function(values) {
console.log(values);
});
// expected output: Array [3, 42, "foo"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment