"use strict";
async function addStuff(a) {
console.log(a);
return a + 1;
}
const aList = [1,2,3]
(async () => {
console.log('aaaaa')
await addStuff(1);
await addStuff(1);
await addStuff(1);
//now I want to loop something
aList.forEach(item => {
await addStuff(item);
})
await addStuff(1);
})()
Likely what I wrote can be wrong, since I am still not familiar with how async functions are chained yet...
So what I attempt to do is,
as addStuff() executing with await, then there will be a list I need to loop at 3rd await, and continue the await addStuff with new stuff in the loop. But currently, each item when iterated, are considered as undefined
.
Is this something impossible to do with async and await? I also thought of let the forEach become the outter wrapper of the async logic, but that will make the process not very flexible.
Any idea or recommendation will be great!
To make the simulation process easier, I built the codepen to play around.