简体   繁体   English

Promise.all().then() 解决了吗?

[英]Promise.all().then() resolve?

Using Node 4.x.使用节点 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() ?当你有一个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() .但我不确定如何将数据获取到第二个.then() I can't use resolve(...) in the first .then() .我不能在第一个.then()使用resolve(...) .then() I figured out I can do this:我发现我可以这样做:

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?但这似乎不是正确的方法......什么是正确的方法?

But that doesn't seem like the proper way to do it..但这似乎不是正确的方法..

That is indeed the proper way to do it (or at least a proper 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, always either 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. ...因为第一个处理程序将两个承诺( 12 )的分辨率作为一个数组,然后创建一个新数组,其中每个都乘以 10 并返回它。 The second handler gets what the first handler returned.第二个处理程序获取第一个处理程序返回的内容。

If the additional work you're doing is synchronous, you can also put it in the 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. ...但如果它是异步的,您将不想这样做,因为它最终会被嵌套,并且嵌套会很快失控。

Today NodeJS supports new async/await syntax.现在 NodeJS 支持新的async/await语法。 This is an easy syntax and makes the life much easier这是一个简单的语法,使生活更轻松

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:了解更多:

Your return data approach is correct, that's an example of promise chaining .您的return data方法是正确的,这是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.如果您从.then()回调中返回一个承诺,JavaScript 将解析该承诺并将数据传递给下一个then()回调。

Just be careful and make sure you handle errors with .catch() .请小心并确保使用.catch()处理错误。 Promise.all() rejects as soon as one of the promises in the array rejects . Promise.all()会在数组中的一个Promise.all()拒绝后立即拒绝

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM