Skip to content

Instantly share code, notes, and snippets.

@cancerberoSgx
Last active May 14, 2018 03:56
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 cancerberoSgx/07da94a4c52fbe3383135575736616fb to your computer and use it in GitHub Desktop.
Save cancerberoSgx/07da94a4c52fbe3383135575736616fb to your computer and use it in GitHub Desktop.
async/await doubt unexpected
function main(arr) {
arr.map(async (o) => {
const result = await g(o); // I expect it to await here but it doesn't
});
}
function g(o) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(o);
}, o.t);
});
}
main([{ s: '1', t: 1000 }, { s: '2', t: 500 }]);
/* prints:
"2"
"1"
I expect it prints "1" first and then "2" because of the await in "const result = await g(o);" .
What I'm missing here ? is it because arr.map is not async ? Thanks
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment