U
U
user_of_toster2021-08-12 12:18:50
Node.js
user_of_toster, 2021-08-12 12:18:50

Do I understand the work of Promise.all correctly?

Option 1 Option 2
[1,2,3,4,5].map(num => heavyIOfunction);

Promise.all([1,2,3,4,5].map(num => heavyIOfunction));


Do I understand correctly that there is no difference between the two options and IO operations start to be executed in parallel from the heavyIOfunction call itself, and not from Promise.all? That is, Promise.all does not start running IO. Promise.all only registers a promise on the result of calling all IO operations.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2021-08-12
@user_of_toster

Do I understand correctly that there is no difference between the two options and IO operations start to be executed in parallel from the heavyIOfunction call itself, and not from Promise.all?

That's right, but not in parallel, the process is one, to whom the answer will come faster (request, reading a file, etc.), that and slippers.
Promise.all only registers a promise on the result of calling all IO operations.

await Promise.all(arrOfPromises);
=
await arrOfPromises.reduce(async (acc, promise) => {
  await acc;
  return promise;
}, Promise.resolve());

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question