M
M
mr jeery2019-03-28 12:28:03
JavaScript
mr jeery, 2019-03-28 12:28:03

Is it parallel execution of promises?

There are two prescriptions

const promise1 = new Promise(...)
const promise2 = new Promise(...)

I want to execute them in parallel, what can you say about the difference between these two methods, will the execution be parallel in the first method ?
const [result1, result2] = [await promise1, await promise2]

OR
const [result1, result2] = Promise.all([promise1, promise2])

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2019-03-28
@jeerjmin

will the execution be parallel in the first method ?

In your code, parallel execution is done by calling:
const promise1 = new Promise(...)
const promise2 = new Promise(...)

since Promises start executing the executor function right after they are created.
But Promise-based libraries don't work like that, they usually return a Promise on a specific call:
In this case:
// последовательный вызов 
const [result1, result2] = [await someCall(), await someOtherCall()];

// параллельный вызов
const [result1, result2] = Promise.all([someCall(), someOtherCall()]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question