我试图从承诺中返回承诺,然后像这样运行Promise.all:
updateVideos()
.then(videos => {
return videos.map(video => updateUrl({ id: video, url: "http://..." }))
})
.then(Promise.all) // throw Promise.all called on non-object我如何使用这种Promise.all。我知道.then(promises => Promise.all(promises))很管用。但我只是想知道为什么失败了。
在Express res.json中也会发生这种情况。错误信息是不同的,但我认为原因是相同的。
例如:
promise().then(res.json) // Cannot read property 'app' of undefined不起作用但
promise().then(results =>res.json(results))确实如此。
发布于 2021-07-23 04:37:24
TJ Crowder的回答解释了为什么会发生这种情况。但是,如果您正在寻找不同的解决方案,BluebirdJS ( npm承诺库)对这种情况的处理就会有所不同。下面的代码对我来说很好。
使用蓝知更鸟也有助于处理需要执行和评估的承诺。mapSeries一直是我的救生者.
import * as Promise from "bluebird"
// ...
Promise.resolve()
.then(() => arrayOfPromises)
.then(Promise.all)https://stackoverflow.com/questions/48399756
复制相似问题