loop async javascript

JavaScript
// Series loop
async (items) => {
  for (let i = 0; i < items.length; i++) {
  	
    // for loop will wait for promise to resolve
    const result = await db.get(items[i]);
    console.log(result);
  }
}

// Parallel loop
async (items) => {
  let promises = [];
  
  // all promises will be added to array in order
  for (let i = 0; i < items.length; i++) {
    promises.push(db.get(items[i]));
  }
  
  // Promise.all will await all promises in the array to resolve
  // then it will itself resolve to an array of the results. 
  // results will be in order of the Promises passed, 
  // regardless of completion order
  
  const results = await Promise.all(promises);
  console.log(results);
}async function processArray(array) {
  // map array to promises
  const promises = array.map(delayedLog);
  // wait until all promises are resolved
  await Promise.all(promises);
  console.log('Done!');
}const mapLoop = async _ => {
  console.log('Start')

  const numFruits = await fruitsToGet.map(async fruit => {
    const numFruit = await getNumFruit(fruit)
    return numFruit
  })

  console.log(numFruits)

  console.log('End')
}
const mapLoop = async _ => {
  console.log('Start')

  const promises = fruitsToGet.map(async fruit => {
    const numFruit = await getNumFruit(fruit)
    return numFruit
  })

  const numFruits = await Promise.all(promises)
  console.log(numFruits)

  console.log('End')
}
const forEachLoop = _ => {
  console.log('Start')

  fruitsToGet.forEach(async fruit => {
    const numFruit = await getNumFruit(fruit)
    console.log(numFruit)
  })

  console.log('End')
}

Source

Also in JavaScript: