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)
You can't run JavaScript code in parallel without using a worker thread or similar. (Here I'm using "parallel" in the sense that two bits of code are literally executing at the same time; you can have interleaved code where two series of operations are taking place, each moving forward while the other waits, where they trade off who waits via promise completions.) Without a worker thread or similar, 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
asyncfunctions 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.alldoesn'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.allwill 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 Promisecall. 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 Promisethere at all. Assuming that "someSynchronous operations" code callsresolveorrejectat some stage, by the time you callPromise.allat 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
thenon the promise (explicitly, or implicitly viaawait). Thankfully these are rare. Standard promises in JavaScript do not behave that way.