Skip to content

How to use Async and Await with

The Valley of Code

Your Web Development Manual

Using async/await combined with map() can be a little tricky. Find out how.

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] // array filled with values

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

const doSomethingAsync = async item => {
  return functionThatReturnsAPromise(item)

const getData = async () => {
  return Promise.all( => doSomethingAsync(item)))

getData().then(data => {

The main thing to notice is the use of Promise.all(), which resolves when all its promises are resolved. 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.

It can be difficult to visualize the example with those placeholder function names, so a simple example of how to use this technique is this Prisma data deletion function I wrote for a Twitter clone to first delete tweets and then users:

export const clearData = async (prisma) => {
  const users = await prisma.user.findMany({})
  const tweets = await prisma.tweet.findMany({})

  const deleteUser = async (user) => {
    return await prisma.user.delete({
      where: { id: }
  const deleteTweet = async (tweet) => {
    return await prisma.tweet.delete({
      where: { id: }

  const deleteTweets = async () => {
    return Promise.all( => deleteTweet(tweet)))

  const deleteUsers = async () => {
    return Promise.all( => deleteUser(user)))

  deleteTweets().then(() => {

Technically this could be much easier summarized as

export const clearData = async (prisma) => {
  await prisma.tweet.deleteMany({})
  await prisma.user.deleteMany({})

but the above code is also valid, and shows how to use promises in, which is the point of this tutorial.

→ Get my JavaScript Beginner's Handbook
→ Read my JavaScript Tutorials on The Valley of Code
→ Read my TypeScript Tutorial on The Valley of Code
  • THE VALLEY OF CODE (+ PRO), your web development manual
  • I wrote 15+ free coding BOOKS, download them here
  • SOLOPRENEUR LAND the missing MBA for wannabe solopreneurs craving a life with more freedom, control, fulfillment and purpose (summer 2024)