Javascript Promise.all().then() 解决了吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33073509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Promise.all().then() resolve?
提问by Jake Wilson
Using Node 4.x. When you have a Promise.all(promises).then()
what is the proper way to resolve the data and pass it to the next .then()
?
使用节点 4.x。当您有一个Promise.all(promises).then()
解析数据并将其传递给下一个的正确方法是什么.then()
?
I want to do something like this:
我想做这样的事情:
Promise.all(promises).then(function(data){
// Do something with the data here
}).then(function(data){
// Do more stuff here
});
But I'm not sure how to get the data to the 2nd .then()
. I can't use resolve(...)
in the first .then()
. I figured out I can do this:
但我不确定如何将数据发送到 2nd .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
});
But that doesn't seem like the proper way to do it... What is the right approach to this?
但这似乎不是正确的方法......什么是正确的方法?
回答by T.J. Crowder
But that doesn't seem like the proper way to do it..
但这似乎不是正确的方法..
That is indeed the proper way to do it (or at least aproper way to do it). This is a key aspect of promises, they're a pipeline, and the data can be massaged by the various handlers in the pipeline.
这确实是正确的做法(或至少是正确的做法)。这是 Promise 的一个关键方面,它们是一个管道,数据可以由管道中的各种处理程序进行处理。
Example:
例子:
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);
});
(catch
handler omitted for brevity. In production code, alwayseither propagate the promise, or handle rejection.)
(catch
为简洁起见省略了处理程序。在生产代码中,始终要么传播承诺,要么处理拒绝。)
The output we see from that is:
我们从中看到的输出是:
First handler [1,2] Second handler [10,20]
...because the first handler gets the resolution of the two promises (1
and 2
) as an array, and then creates a new array with each of those multiplied by 10 and returns it. The second handler gets what the first handler returned.
...因为第一个处理程序将两个 promise ( 1
and 2
)的解析作为一个数组,然后创建一个新数组,其中的每一个都乘以 10 并返回它。第二个处理程序获取第一个处理程序返回的内容。
If the additional work you're doing is synchronous, you can also put it inthe first handler:
如果你正在做的额外工作是同步的,你也可以把它放在第一个处理程序中:
Example:
例子:
const promises = [
new Promise(resolve => setTimeout(resolve, 0, 1)),
new Promise(resolve => setTimeout(resolve, 0, 2))
];
Promise.all(promises)
.then(data => {
console.log("Initial data", data);
data = data.map(entry => entry * 10);
console.log("Updated data", data);
return data;
});
...but if it's asynchronous you won't want to do that as it ends up getting nested, and the nesting can quickly get out of hand.
...但如果它是异步的,您将不想这样做,因为它最终会被嵌套,并且嵌套会很快失控。
回答by Aminadav Glickshtein
Today NodeJS supports new async/await
syntax. This is an easy syntax and makes the life much easier
现在 NodeJS 支持新的async/await
语法。这是一个简单的语法,使生活更轻松
async function process(promises) { // must be an async function
let x = await Promise.all(promises); // now x will be an array
x = x.map( tmp => tmp * 10); // proccessing the data.
}
const promises = [
new Promise(resolve => setTimeout(resolve, 0, 1)),
new Promise(resolve => setTimeout(resolve, 0, 2))
];
process(promises)
Learn more:
了解更多:
回答by vkarpov15
Your return data
approach is correct, that's an example of promise chaining. If you return a promise from your .then()
callback, JavaScript will resolve that promise and pass the data to the next then()
callback.
你的return data
方法是正确的,这是promise chaining的一个例子。如果您从.then()
回调中返回一个承诺,JavaScript 将解析该承诺并将数据传递给下一个then()
回调。
Just be careful and make sure you handle errors with .catch()
. Promise.all()
rejects as soon as one of the promises in the array rejects.
请小心并确保使用.catch()
. Promise.all()
一旦数组中的一个 promise 拒绝,就拒绝。