user1
user1

Reputation: 586

Run multiple asynchronous methods together

Is there a way to run together three methods and then assign results to an object? I've tried the example below, but it doesn't work.

const checkResults: IMyType = {
            chk1: await startChk1.run(),
            chk2: await startChk2.run(),
            chk3: await startChk3.run(),
        };


 Promise.all(
            [chk1.run(), chk2.run()],
            chk3.run()
        ).then((results) => {
            const theResults: IMyType = {
                chk1: results[0],
                chk2: results[1],
                chk3: results[2]
            };
        });

Upvotes: 0

Views: 25

Answers (1)

Darren G
Darren G

Reputation: 878

Something like the following will run 3 methods at once and return values to 3 variables:

let [ var1, var2, var3 ] = await Promise.all([chk1.run(), chk2.run(), chk3.run()])

If you want to assign the results to an array you could replace the 3 variables with an array name e.g. let chkArray = ...

Upvotes: 1

Related Questions