How to use Async and Await with Array.prototype.map()

249 阅读1分钟
原文链接: flaviocopes.com

You want to execute an async function inside a map() call, to perform an operation on every element of the array, and get the results back.

How can you do so?

This is the correct syntax:

const list = [1, 2, 3, 4, 5] //...an array filled with values

const functionWithPromise = item => { //a function that returns a promise
  return Promise.resolve('ok')
}

const anAsyncFunction = async item => {
  return functionWithPromise(item)
}

const getData = async () => {
  return Promise.all(list.map(item => anAsyncFunction(item)))
}

getData().then(data => {
  console.log(data)
})

The main thing to notice is the use of Promise.all(), which resolves when all its promises are resolved.

list.map() returns a list of promises, so in result we’ll get the value when everything we ran is resolved.

Remember, we must wrap any code that calls await in an async function.

See the promises article for more on promises, and the async/await guide.


🔥 Preorder my ebook The Developer's Guide to Having a Successful Blog 🔥

Download my free JavaScript book!