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()