loops in async function code example
Example 1: async await in forloops
async (items) => {
for (let i = 0; i < items.length; i++) {
const result = await db.get(items[i]);
console.log(result);
}
}
async (items) => {
let promises = [];
for (let i = 0; i < items.length; i++) {
promises.push(db.get(items[i]));
}
const results = await Promise.all(promises);
console.log(results);
}
Example 2: wait for loop to finish javascript
async function processArray(array) {
const promises = array.map(delayedLog);
await Promise.all(promises);
console.log('Done!');
}