Created
April 17, 2019 06:33
-
-
Save indexzero/131d053c958e49b87ad3f179e76a0d33 to your computer and use it in GitHub Desktop.
Promise.all proof by doing
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
Start | 1 | foo | |
Start | 2 | bar | |
Start | 3 | bazz | |
End | 2 | bar | |
End | 3 | bazz | |
End | 1 | foo | |
[ 'foo', 'bar', 'bazz' ] |
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
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