Promise.all()。然后()解决?
使用节点4.x. 当你有一个Promise.all(promises).then()
什么是解决数据的正确方法,并传递给下一个.then()
?
我想要做这样的事情:
Promise.all(promises).then(function(data){ // Do something with the data here }).then(function(data){ // Do more stuff here });
但是我不知道如何获取数据到第二个.then()
。 我不能在第一个.then()
使用resolve(...)
.then()
。 我想我可以做到这一点:
return Promise.all(promises).then(function(data){ // Do something with the data here return data; }).then(function(data){ // Do more stuff here });
但是,这似乎不是正确的方法来做到这一点…什么是正确的方法呢?
但是,这似乎不是正确的方式来做到这一点..
这确实是这样做的正确方法。 这是承诺的一个关键方面,它们是一个pipe道,数据可以通过pipe道中的各种处理程序进行处理。
例:
const promises = [ new Promise(resolve => setTimeout(resolve, 0, 1)), new Promise(resolve => setTimeout(resolve, 0, 2)) ]; Promise.all(promises) .then(data => { console.log("First handler", data); return data.map(entry => entry * 10); }) .then(data => { console.log("Second handler", data); });