JavaScript: async/await with forEach

tipjs,async,foreach,function

JavaScript: async/await with forEach()

const waitFor = (ms) => new Promise(r => setTimeout(r, ms))
const asyncForEach = async (array, callback) => {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array)
  }
}

const start = async () => {
  await asyncForEach([1, 2, 3], async (num) => {
    await waitFor(50)
    console.log(num)
  })
  console.log('Done')
}

start()

Có thể bạn đã miss một số snippets code

Object.keys và Object.getOwnPropertyNames

Object.keys và Object.getOwnPropertyNames

promise in javascript

promise in javascript

Spread Operator for Objects

Using the spread operator during an Object declaration will assign the properties of the referenced Object to the new Object.

Await in a for loop

How to Await in a for loop

Đếm số phần tử trong mảng JavaScript

Đếm số lần xuất hiện của các phần tử trong mảng JavaScript