Answers for "async for loop"

2

async for loop

(async function loop() {
    for (let i = 0; i < 10; i++) {
        await new Promise(resolve => setTimeout(resolve, Math.random() * 1000));
        console.log(i);
    }
})();
Posted by: Guest on August-19-2020
2

async await in forloops

// 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);
}
Posted by: Guest on May-15-2020
0

loop through async javascript -3

var j = 10;
for (var i = 0; i < j; i++) {
    asynchronousProcess(i, function(cntr) {
        console.log(cntr);
    });
}
Posted by: Guest on September-08-2021
-2

loop async javascript

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')
}
Posted by: Guest on July-03-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language