Promise.all().then() 解决了吗? [英] Promise.all().then() resolve?

查看:24
本文介绍了Promise.all().then() 解决了吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用节点 4.x.当你有一个 Promise.all(promises).then() 时,解析数据并将其传递给下一个 .then() 的正确方法是什么?>

我想做这样的事情:

Promise.all(promises).then(function(data){//对这里的数据做一些事情}).then(函数(数据){//在这里做更多的事情});

但我不确定如何将数据获取到第二个 .then().我不能在第一个 .then() 中使用 resolve(...).我发现我可以做到这一点:

return Promise.all(promises).then(function(data){//对这里的数据做一些事情返回数据;}).then(函数(数据){//在这里做更多的事情});

但这似乎不是正确的方法……正确的方法是什么?

解决方案

但这似乎不是正确的方法..

这确实是正确的做法(或至少一种正确的做法).这是 Promise 的一个关键方面,它们是一个管道,数据可以由管道中的各种处理程序进行处理.

示例:

const promises = [new Promise(resolve => setTimeout(resolve, 0, 1)),new Promise(resolve => setTimeout(resolve, 0, 2))];Promise.all(承诺).then(数据=> {console.log("第一个处理程序", 数据);返回 data.map(entry => entry * 10);}).then(数据=> {console.log("第二个处理程序", 数据);});

(为简洁起见省略了catch处理程序.在生产代码中,总是要么传播承诺,要么处理拒绝.)

我们从中看到的输出是:

<前>第一个处理程序 [1,2]第二个处理程序 [10,20]

...因为第一个处理程序将两个 promise(12)的解析作为一个数组,然后使用其中的每一个创建一个新数组乘以 10 并返回它.第二个处理程序获取第一个处理程序返回的内容.

如果你正在做的额外工作是同步的,你也可以把它放在第一个处理程序中:

示例:

const promises = [new Promise(resolve => setTimeout(resolve, 0, 1)),new Promise(resolve => setTimeout(resolve, 0, 2))];Promise.all(承诺).then(数据=> {console.log("初始数据", 数据);data = data.map(entry => entry * 10);console.log("更新数据", data);返回数据;});

...但是如果它是异步的,你就不会想要这样做,因为它最终会被嵌套,并且嵌套很快就会失控.

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()?

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:

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.

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.)

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.

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.

这篇关于Promise.all().then() 解决了吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆