// The modern version of a for loop, "for of” which does await for promises.
async function test() {
const array = [2,1,3];
for (const item of array) {
let result = await doAsyncAction(item);
console.log(result);
}
}
test();
// Traditional loop
async function test() {
const array = [2,1,3];
for (let i = 0; i < array.length; i++) {
let result = await doAsyncAction(array[i]);
console.log(result);
}
}
test();
// common function for example ;-)
function doAsyncAction(num) {
return new Promise((resolve) => {
setTimeout(() => {
resolve(num + 'a');
}, 2000);
});
}