当我推送承诺数组时,承诺是否已解决?
Is the promise resolved, when I push promise array?
我想将 promise 推送到数组。然后我想用Promise.all()
来解决它。但我不确定,当我推送到数组时,promise 是否有效?
例如:
const productIds= [1,2,3,4,5,6] // example
const promises = [];
productIds.map(productId => promises.push(ProductDataAccess.updateProductStock(store,productId,incQuery)));
如果我不Promise.all(),会发生数据库进程吗?或者当我制作 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);
是的,promises 运行 在它们被创建的时候,而不是在它们被等待的时候(无论是 Promise.all()
还是其他)。
你会(不管Promise.all
的用法如何),但不会等他们。
如果此代码块在 async
函数中,您可以 await
这种方式的承诺(而且,您可以简单地使用 map
,而不使用 push
,构建你需要的承诺数组):
const productIds = [1,2,3,4,5,6];
const promises = productIds.map((productId) => ProductDataAccess.updateProductStock(store,productId,incQuery));
await Promise.all(promises);
If I don't Promise.all(), will db process occur?
视情况而定。一般的回答是是。但请注意,这并不总是正确的。
通常,return Promise 会在您调用它们时安排一个异步过程。所以异步过程会发生,不管你在等他们。
但是,有些函数并不是真正的 return 承诺。他们 return 一个类似承诺的对象。有时(不总是)它们不会启动异步过程,除非您调用 .then()
。使用 fluent/chainable 函数的数据库库就是这样做的。这是数据库库中常见的设计模式:
// 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!
许多数据库都这样做,以实现对用户透明的 fluent/chaining 样式 API。他们通过调用 .then()
函数来检测查询构造的结束。
现在,我不知道您使用的是什么数据库,但是如果您受此影响,那么要触发数据库查询过程,您需要调用then
直接或间接:
- 给自己打电话
.then()
- 将对象传递给内部调用
.then()
的 Promise.all()
await
内部调用 .then()
的 async
函数的结果
我想将 promise 推送到数组。然后我想用Promise.all()
来解决它。但我不确定,当我推送到数组时,promise 是否有效?
例如:
const productIds= [1,2,3,4,5,6] // example
const promises = [];
productIds.map(productId => promises.push(ProductDataAccess.updateProductStock(store,productId,incQuery)));
如果我不Promise.all(),会发生数据库进程吗?或者当我制作 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);
是的,promises 运行 在它们被创建的时候,而不是在它们被等待的时候(无论是 Promise.all()
还是其他)。
你会Promise.all
的用法如何),但不会等他们。
如果此代码块在 async
函数中,您可以 await
这种方式的承诺(而且,您可以简单地使用 map
,而不使用 push
,构建你需要的承诺数组):
const productIds = [1,2,3,4,5,6];
const promises = productIds.map((productId) => ProductDataAccess.updateProductStock(store,productId,incQuery));
await Promise.all(promises);
If I don't Promise.all(), will db process occur?
视情况而定。一般的回答是是。但请注意,这并不总是正确的。
通常,return Promise 会在您调用它们时安排一个异步过程。所以异步过程会发生,不管你在等他们。
但是,有些函数并不是真正的 return 承诺。他们 return 一个类似承诺的对象。有时(不总是)它们不会启动异步过程,除非您调用 .then()
。使用 fluent/chainable 函数的数据库库就是这样做的。这是数据库库中常见的设计模式:
// 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!
许多数据库都这样做,以实现对用户透明的 fluent/chaining 样式 API。他们通过调用 .then()
函数来检测查询构造的结束。
现在,我不知道您使用的是什么数据库,但是如果您受此影响,那么要触发数据库查询过程,您需要调用then
直接或间接:
- 给自己打电话
.then()
- 将对象传递给内部调用
.then()
的 await
内部调用.then()
的
Promise.all()
async
函数的结果