Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript async/await for Promises inside Array.map() [duplicate]

Given the following code

class SomeClass {
  async someFunc() {
    const urlParameters = [0, 1, 2];
    const batchAjaxResult = await urlParameters.map((parameter) => { 
        const result = await $.get(`someUrl/${parameter}`);
        return {
          parameter,
          result
        }
     });

  console.log(batchAjaxResult);

  }
}

JavaScript will return an Array of resolved Promises instead of the actual Promises result.

This is probably due to Array.map() not being implemented as a Promise.

Is there a Promise-based version of Array.map?

This is question differs from How to return the response from an asynchronous call, because it's about How to return batched responses wrapped inside Array.map.

like image 711
Tobias Mühl Avatar asked Apr 09 '26 08:04

Tobias Mühl


2 Answers

You could use this simple function which chains the promises to achieve sequential execution:

function asyncMap(arr, mapper) {
  var q = Promise.resolve();
  return Promise.all(arr.map(v => q = q.then(() => mapper(v))));
}


// Usage

asyncMap([1, 2, 3], v => {
  return new Promise(resolve => {
    console.log("Start " + v);
    setTimeout(() => {
      console.log("End " + v);
      resolve("foo" + v);
    }, 500);
  });
}).then(a => console.log(a));
like image 90
Tamas Hegedus Avatar answered Apr 10 '26 22:04

Tamas Hegedus


By writing this question I managed to get down to the root of the problem.

It seems like Bluebird has such an feature called Promise.map

Similarily there is a native implementation called Promise.all that creates a "bulk Promise"

I changed the code as follows and it's working:

class SomeClass {
  async someFunc() {
      const urlParameters = [0, 1, 2];
      const batchAjaxResult = await Promise.all(
          urlParameters.map(async (parameter) => {
            const result = await $.get(`someUrl/${parameter}`);
            return {
              parameter,
              result
            }
         })
      );
      console.log(batchAjaxResult);

  }
}
like image 24
Tobias Mühl Avatar answered Apr 10 '26 22:04

Tobias Mühl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!