Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does passing in an array of async functions to Promise.all work as expected?

await Promise.all(
  arr_tar.map(async (tar) => {
    return {
      n: await tar_get_one(tar),
      name: tar.name,
    };
  })
)

Why is the code above. There elements of the input array are async functions, but they are not invoked by anything.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

According to MDN:

The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.

In the code shown, Array#map will call the async callback function on each element of the array and since async functions return Promises the result will be an array of Promises. Promise.all takes an iterable of Promises as input so this works.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading