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

92
Views
Promise.all to key value results

I was wondering if there is an elegant way to achieve the following:

const res = await Promise.all([taskA, taskB])

Where res will become:

{
taskA: <result of taskA>,
taskB: <result of taskB>
}

(The main reason I'm using Promise.all is because I want to run those tasks in parallel, even though they are distinct in nature).

almost 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Here is an option if taskA and taskB can be hardcoded and you are ok with shadowing variables.

const res = await Promise.all([taskA, taskB]).then(([taskA, taskB]) => ({taskA, taskB}));

But it would be pretty similar to:

const [tA, tB] = await Promise.all([taskA, taskB]);
const res = {taskA: tA, taskB: tB};
almost 4 years ago · Santiago Trujillo Report

0

Totally possible. First, what you need to do is map through all of the tasks you want to do, returning a new array of promises that generate an entry name based on the name of the function and an entry value based on the awaited result:

const taskA = async () => Promise.resolve().then(() => 'foo');
const taskB = async () => Promise.resolve('bar');

(async () => {
    const results = Object.fromEntries(await Promise.all([taskA, taskB].map((func) => (async () => [func.name, await func()])())));

    console.log(results);
})();

Here's something with a bit more explanation (and easier to read):

const taskA = async () => Promise.resolve().then(() => 'foo');
const taskB = async () => Promise.resolve('bar');

const prettyPromiseAll = async (funcs: Array<(...args: any) => Promise<any>>) => {
    // Since we're generating an array of entries, we can create an object from them
    // once all promises have been awaited.
    return Object.fromEntries(
        // Call Promise.all as normal, making sure to await it.
        await Promise.all(
            // But, for each function...
            funcs.map((func) => {
                // Return an automatically invoked async function (returning a promise)
                // that returns a tuple looking like [nameOfFunction, resultOfPromise]
                return (async () => [func.name, await func()])();
            })
        )
    );
};

(async () => {
    const results = await prettyPromiseAll([taskA, taskB]);

    console.log(results);
})();

Why is this better than the answer below? Because in the answer below, you need to specify the keys for which you'd like the results to have. This method uses the function name.

Note:

This prettyPromiseAll function works differently from regular Promise.all, as it takes an array of functions that return promises, rather than an array of promises.

almost 4 years ago · Santiago Trujillo 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!