Reputation: 596
I'm led to believe that Promise.all executes all the functions you pass it in parallel and doesn't care what order the returned promises finish.
But when I write this test code:
function Promise1(){
return new Promise(function(resolve, reject){
for(let i = 0; i < 10; i++){
console.log("Done Err!");
}
resolve(true)
})
}
function Promise2(){
return new Promise(function(resolve, reject){
for(let i = 0; i < 10; i++){
console.log("Done True!");
}
resolve(true)
})
}
Promise.all([
Promise1(),
Promise2()
])
.then(function(){
console.log("All Done!")
})
The result I get is this
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done Err!
Done True!
Done True!
Done True!
Done True!
Done True!
Done True!
Done True!
Done True!
Done True!
Done True!
Done!
But if they're running in parallel wouldn't I expect them to be executing at the same time and give me a result like this?
Done Err!
Done True!
Done Err!
Done True!
Done Err!
Done True!
Done Err!
Done True!
Etc. Etc.?
Or am I missing something in the way I'm doing it?
Upvotes: 25
Views: 31958
Reputation: 3342
It's because your Promises are blocking and synchronous! Try something with a timeout instead of a synchronous loop:
function randomResolve(name) {
return new Promise(resolve => setTimeout(() => {
console.log(name);
resolve();
}, 100 * Math.random()));
}
Promise.all([
randomResolve(1),
randomResolve(2),
randomResolve(3),
randomResolve(4),
])
.then(function(){
console.log("All Done!")
})
Upvotes: 19
Reputation: 4267
To build on what Johannes Merz started, I propose this code to clarify that things are happening in parallel.
JS is single-threaded, but Node.js has plenty of tools for launching additional threads explicitly and implicitly. Promises expose more functionality we often need without having to launch new threads or processes explicitly. Promise.all() is such an example, but you need to be comfortable with Promises to use it without creating serious headaches for yourself, such as Promise Scope memory leaks.
function randomResolve(name,t) {
return new Promise(resolve => setTimeout(() => {
console.log({ name, t });
resolve({ name, t });
}, t));
}
(() => {
// Get epoch time before starting so we can confirm the execution time reflects our slowest timeout
let start = new Date().valueOf();
Promise.all([
randomResolve(1, 1000 * Math.random()),
randomResolve(2, 1000 * Math.random()),
randomResolve(3, 1000 * Math.random()),
randomResolve(4, 1000 * Math.random()),
])
.then(function( res ){
console.info( res );
console.log("All Done!", parseInt(new Date().valueOf() - start) );
})
})();
This pattern takes an array of inputs and uses array.map() to get back an array of launched promises which will be parallel processed as above. Notice there is NO use of async/await here.
function randomResolve(name,t) {
return new Promise(resolve => setTimeout(() => {
console.log({ name, t });
resolve({ name, t });
}, t));
}
(() => {
// Get epoch time before starting so we can confirm the execution time reflects our slowest timeout
let start = new Date().valueOf(),
vals = [
[1, 1000 * Math.random()],
[2, 1000 * Math.random()],
[3, 1000 * Math.random()],
[4, 1000 * Math.random()]
];
Promise.all( vals.map( v => { return randomResolve(v[0], v[1] ); } ) )
.then(function( res ){
console.info( res );
console.log("All Done!", parseInt(new Date().valueOf() - start) );
})
})();
This version does implement async/await.
function randomResolve(name,t) {
return new Promise(resolve => setTimeout(() => {
console.log({ name, t });
resolve({ name, t });
}, t));
}
(async () => {
// Get epoch time before starting so we can confirm the execution time reflects our slowest timeout
let start = new Date().valueOf(),
vals = [
[1, 1000 * Math.random()],
[2, 1000 * Math.random()],
[3, 1000 * Math.random()],
[4, 1000 * Math.random()]
];
let res = await Promise.all( vals.map( async v => { return await randomResolve( v[0], v[1] ); } ) );
// await the Promise.aall() call instead of using .then() afterwards with another closure then
// forEach v in vals, start and await a Promise from randomResolve() then return the result to map
console.info( res );
console.log("All Done!", parseInt(new Date().valueOf() - start) );
})();
Upvotes: 3
Reputation: 2934
Maybe the solution would be to use worker-farm, I wouldn't know how to explain it because I'm new to NodeJS, but here's an interesting article about it:
In this case, here's how you could use worker-farm
(I don't have a good grasp of the subject at the moment so don't hesitate to correct me.)
First, install "worker-farm"
npm install --save worker-farm
then:
// index.js
const workerFarm = require("worker-farm");
const service = workerFarm(require.resolve("./service"));
function Promise1() {
return new Promise(function(resolve, reject) {
service("promise1", (err, output) => {
resolve(true);
});
});
}
function Promise2() {
return new Promise(function(resolve, reject) {
service("promise2", (err, output) => {
resolve(true);
});
});
}
Promise.all([Promise1(), Promise2()]).then(function() {
console.log("All Done!");
});
In service.js
create a function that takes a lot of execution time, execute the callback once the execution is finished.
// service.js
const blocker = (input, callback) => {
// Number loop turns
// Adjust this number depending on your CPU
const nbTurn = 1000000000;
// How many log to display during the loop
const nbReminder = 4;
let i;
for (i = 0; i <= nbTurn; i++) {
const remainder = (i % Math.ceil(nbTurn / nbReminder)) / 100;
if (remainder === 0) {
console.log(input, i);
}
}
console.log(input + "end", i);
callback(null, nbTurn);
};
module.exports = blocker;
EDIT: I found another solution: napajs (working example on codesandbox)
Upvotes: 1
Reputation: 18755
I'd suggest to use it like this:
const [
res1,
res2
] = await Promise.all([
asyncCall1(),
asyncCall1(),
]);
Upvotes: 6
Reputation: 6895
A non-Async body is executed serially The moment you reach a Async call within your body (Ex. Hit a URL), Other Promises in the array will start executing.
Upvotes: 1