What do think this JS code prints?
const arr = [3, 1, 2];
const wait = (ms) => new Promise(resolve => {
setTimeout(resolve, ms);
});
arr.forEach(async item => {
await wait(item * 100);
console.log(item);
})
How about this?
const arr = [3, 1, 2];
const wait = (ms) => new Promise(resolve => {
setTimeout(resolve, ms);
});
const mapped = arr.map(async item => {
await wait(item * 100);
return item * 2;
})
console.log(mapped);
4 responses to “async / await”
At a guess
3
1
2
Then
6
2
4
LikeLike
I checked:
1
2
3
Then
[ Promise { },
Promise { },
Promise { } ]
LikeLike
Async `map` doesn’t seem very helpful 🙃
LikeLike
async `forEach` isn’t quite intuitive either. 🙂
LikeLike