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

Rest Operator for Objects

Now we can use rest on the properties of an Object. It allows us to explicitly extract certain named variables, and assign any uncalled variables into a catchall Object.

10 mẹo sử dụng array và object trong javascript

10 mẹo sử dụng array và object trong javascript

Merge Array

How to combine two sorted arrays into one? We can do this easily with the spread operator.

lỗi access-control-allow-origin và cách khắc phục

lỗi access-control-allow-origin và cách khắc phục trong express, nodejs

kiểm tra email trong javascript

kiểm tra email trong javascript