Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

109
Views
Javascript push and wait for async promises in array

I want to wait for an array of Promises but I cannot add them without executing them. So I had to create a wrapper function, however Promise.all does not execute them.

This is my code:

    const set = new Set(Array.from([1, 2, 3, 4, 5]));

    const func = async (input) => {
        let result = await asyncFunc(input);
        return result;
    }

    var promises = [];
    for(const element of set) {
        promises.push(() => { func(element) });
    }

    await Promise.all(promises).then(values => {
        console.log(values);
    }); 

I exepect Promise.all executes the func function, but it does not. How can I achieve this result?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could just push promises to your array, not functions:

    const set = new Set(Array.from([1, 2, 3, 4, 5]));

    const func = async (input) => {
        let result = await asyncFunc(input);
        return result;
    }

    var promises = [];
    for(const element of set) {
        promises.push(func(element));
    }

    await Promise.all(promises).then(values => {
        console.log(values);
    }); 
about 4 years ago · Juan Pablo Isaza Report

0

I found my problem!

The issue of mine was that some of my promises will reject and Promise.all does not continue when this is the case!

I solved by using Promise.allSettled!

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!