Async call to API within map function (React)

Active3 hr before
Viewed126 times

8 Answers


Stack Overflow Public questions & answers , Stack Overflow help chat ,Instead of using .map() you can use a traditional for or while loop:

Turn your array into an array of promises and then use Promise.all

export const someAction = () => async (dispatch) => {

   try {
      const payload = await Promise.all( => loaders.myResponseApi(param1, id)));
         type: types.ARRAY_API,
   } catch (err) {
      // an error occurred in at least one of the promises

load more v

You can now use this executeAllLongRunningTasks function to log your new array of resolved task responses to the console like this:,You can now be confident when it comes to composing asynchronous functions using! For more information, check out the documentation for async/await syntax.,Or, if you want to use it within an async function or top-level async module:

3 executeLongRunningTask = async () => {
   const response = await fetch(this.taskUrl);
   return await response.json();
load more v

 Pretag team - issue, fix, solve, resolve

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.,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:,Remember, we must wrap any code that calls await in an async function.

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 => {
load more v

This makes the general pattern of an async map to be Promise.all( (...) => ...)).,And second, it needs to wait for all the Promises then collect the results in an Array. Fortunately, the Promise.all built-in call is exactly what we need for step 2.,The async disposer pattern

const arr = [1, 2, 3];

const syncRes = => {
   return i + 1;

// 2,3,4
load more v

But the problem is the API function is returning a promise instead of a result at that time.,So, I tried using async-await inside the map function but the map function returns the promise immediately instead of waiting for the API to resolve and return the correct result.,Below is the sample code to resolve all the asynchronous functions that are inside the map.

Below is the sample code to resolve all the asynchronous functions that are inside the map.

const arr = [1, 2, 3, 4, 5];

async function promises() {

   const unresolved = (idx) => {
      await setTimeout(() => {}, 1000);
      return idx;

   const resolved = await Promise.all(unresolved)



Suppose I have a list of ids, and I want to make an API call on each id.,I recently needed to use async/await inside a map function.,Converting Promises to async/await in JavaScript

const ids = ["id_1", "id_2", "id_3"];
const dataById = => {
   // make API call
load more v

You’re probably used to fetching data in React using axios or fetch. The usual method of handling data fetching is to:,For this tutorial, we will be making use of Create React App. You can create a project by running:,First, we created a function called loadUsers. This will make the API call using the fetch API. And, when it does, it returns a promise which gets resolved. After that, the needed props are made available to the component.

For this tutorial, we will be making use of Create React App. You can create a project by running:

npx create - react - app react - async -demo
load more v

Other "function-react" queries related to "Async call to API within map function (React)"