functionwork(n){returnnewPromise(resolve=>{setTimeout(()=>{resolve(n*10);},200);})}(asyncfunction(){array=[1,2,3];for(leti=0;i<array.length;i++){// it WILL wait here. constresult=awaitwork(array[i]);// result is 10, 20, 30 as expectedconsole.log(result);}})();
async function doesn't work inside
for loop
Instead, you push all promises in an array then resolve them with
Promise.all
Actually,
async
works as expected in for loop.Yes, that's exactly it!