I am exploring async nature of js, and wish to write parallel execution for for
each loop iteration.
But Promise.all() execute sequentially right?, readed at a website. But I tried below implementation. Is it the correct way to achieve the same? like if Promise.all()
is not executing promises parallely how each loop will run parallely.
Thanks.
let asnycOpertions;// array to collect promises
for(let value of iterable) {
asyncOperations.push(return new Promise((res,rej)=>{
// someSynchronous operations
}));
}
Promise.all(asyncOperations)
2
Answers
Promise body is called synchronous. Asynchronous only means that inner async operations would not just wait outer io and stop other parts from running. So it is not multithreading, but can speed things up when you are working with
fs
and want to make bunch of requests to OS API, or make some AJAX calls.You can’t run JavaScript code in parallel without using a worker thread or similar. Without worker thread, you can change when the synchronous JavaScript code runs, but you can’t make it parallel with other JavaScript code in the same realm (loosely: the global environment and the things in it, like the environment for a window on browsers). Without a worker thread, you can have host-provided operations (like network requests) running in parallel, but not JavaScript code itself. The JavaScript specification requires that only a single thread be running code in the realm at any given time, so all JavaScript code in a single realm can be thought of as running on a single thread.
The vast majority of JavaScript is synchronous. It’s used in environments that have a lot of asynchronousness, but only
async
functions and promises cater to that specifically at the JavaScript language level.You can’t, without worker threads. So if the code in the loop is synchronous (as you’ve suggested), you’ll need workers to run it in parallel.
That website is wrong or you misunderstood.
Promise.all
doesn’t execute anything at all. It just observes promises. In the normal case, by the time you have a promise, the asynchronous work the promise will report on is already underway (or finished).¹ If that work is underway (such as network requests made via the host environment), that various pieces of work that the promises you pass intoPromise.all
will report on can be running in parallel.Re your code:
The function you pass to
new Promise
(the promise executor function) is called synchronously during thenew Promise
call. Its job is to start the asynchronous operation by asking the host environment to do something asynchronous. That means the code you have for "someSynchronous operations" will run synchronously, exactly as though you didn’t havenew Promise
there at all. Assuming that "someSynchronous operations" code callsresolve
orreject
at some stage, by the time you callPromise.all
at the end, all of those promises are already settled, because that code ran synchronously.¹ "already underway" – there are a small number of libraries that provide non-standard promises that don’t start their work until you call
then
on the promise (explicitly, or implicitly viaawait
). Thankfully these are rare. Standard promises in JavaScript do not behave that way.