我正在处理一些Azure函数--我正在阅读CosmosDB中的一些文档,对这些文档执行一些操作。
我遇到了下面的代码,但是并不真正理解它。
then(),不能仅仅删除它并在resolve()部分直接执行任何我想要的操作的代码吗?所以:
const promises = documents.map(document =>
Promise.resolve().then(async () => {
// Do some stuff on the document
})
);
return Promise.all(promises).then();与:
const promises = documents.map(document =>
Promise.resolve(async () => {
// Do some stuff on the document
})
);
return Promise.all(promises).then();发布于 2019-05-21 08:33:58
实际上,你不应该使用任何一种,而应该选择
const promises = documents.map(async document => {
// Do some stuff on the document
});您的第一个片段不必要地构造了一个立即解决的承诺,只是将一个函数链接到它。这就推迟了异步进行的计算,这可能是故意的,但应该在注释中提到--我们只需为此使用async document => { await void 0; … },就可以避免将这两种承诺风格混合在一起。
您的第二个片段构建了一个功能实现的承诺--这不是您想要的。
https://stackoverflow.com/questions/56234048
复制相似问题