Last active
May 14, 2018 03:56
-
-
Save cancerberoSgx/07da94a4c52fbe3383135575736616fb to your computer and use it in GitHub Desktop.
async/await doubt unexpected
This file contains 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 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