0

I want to push promise to an array. Then I want to resolve it using Promise.all(). But I'm not sure, is promise work when i push to array?

For example:


const productIds= [1,2,3,4,5,6] // example
const promises = [];
productIds.map(productId => promises.push(ProductDataAccess.updateProductStock(store,productId,incQuery)));

If I don't Promise.all(), will db process occur? Or does the db process occur when I make Promise.all().


const productIds= [1,2,3,4,5,6] // example
const promises = [];
productIds.map(productId => promises.push(ProductDataAccess.updateProductStock(store,productId,incQuery)));
Pormise.all(promises);
norbitrial
  • 14,716
  • 7
  • 32
  • 59
  • if you dont use Promise.all(promises), promise in the promises array will not be executed – Ekansh Jain Mar 08 '20 at 13:52
  • 1
    Promise executes just as you create it. Method `Promise.all` simply helps you wait till all promises have finished executing, provided they all resolve. – vitaly-t Mar 08 '20 at 14:04

3 Answers3

1

Yes, promises run when they are created, not when they are awaited upon (be it with Promise.all() or otherwise).

AKX
  • 152,115
  • 15
  • 115
  • 172
1

You will start the execution of the promises from the moment each promise is created (regardless of the usage of Promise.all), but not wait for them.

If this block of code is in an async function, you can await the promises in this way (and also, you can simply use map, without push, to build the array of promises you need):

const productIds = [1,2,3,4,5,6];
const promises = productIds.map((productId) => ProductDataAccess.updateProductStock(store,productId,incQuery));

await Promise.all(promises);
Alberto Trindade Tavares
  • 10,056
  • 5
  • 38
  • 46
  • 1
    Those points are valid, but the actual question `If I don't Promise.all(), will db process occur? Or does the db process occur when I make Promise.all().` is only indirectly "answered" by `otherwise, you will start the execution of them - from the moment each promise is created -, but not wait for them` – t.niese Mar 08 '20 at 14:04
  • @t.niese Yes, good point! Rearranged my answer a bit upon your feedback. Thanks! – Alberto Trindade Tavares Mar 08 '20 at 14:09
1

If I don't Promise.all(), will db process occur?

It depends. The general answer is yes. But be aware that this is not always true.

Normally, functions that return a Promise schedules an asynchronous process when you call them. So the asynchronous process will happen regardless of you waiting for them.

However, there are some functions that don't really return a promise. They return a promise-like object. And sometimes (not always) they don't start the asynchronous process unless you call .then(). And database libraries that use fluent/chainable functions do this. This is a common design pattern in database libraries:

// knex example:

let x = knex('my_table').select(['id','some_info']); // will not trigger db query
console.log(x); // knex object - not a Promise!

x = x.where('id', 0); // still no db query
console.log(x); // still not a Promise!

x = x.then(result => console.log(result)); // TRIGGERS DB QUERY!
console.log(x); // Yay! A Promise!

Lots of database libraries do this in order to implement a fluent/chaining style API that is transparent to the user. They detect the end of query construction by the .then() function being called.

Now, I don't know what database library you are using but if you are affected by this then to trigger the database query process you will need to call then either directly or indirectly:

  • call .then() yourself
  • pass the object to Promise.all() which internally calls .then()
  • await the result in an async function which internally calls .then()
slebetman
  • 109,858
  • 19
  • 140
  • 171