Promise.All中长期承诺链的最佳实践是什么?

时间:2021-08-25 20:09:27

Suppose I have several main promises, some of which return promise inside. I want to retrieve results from sort of promise functions that why I think about Promise.all. First off, let take a look at my example code below

假设我有几个主要的承诺,其中一些承诺在内部。我想从各种promise函数中检索结果,这就是为什么我会想到Promise.all。首先,让我们看看下面的示例代码

var Promise = require('bluebird');
promise = []
function promise1(){
  return new Promise(function(resolve, reject){
    console.log('promise1');
    resolve('promise1');
  });
}
function promise2(){
  return new Promise(function(resolve, reject) {
    console.log('promise2');
    resolve('promise2');
  });
}
function promise3(){
  promise2().then(function(){
    return new Promise(function(resolve, reject) {
      console.log('promise3');
      resolve('promise3')
    })
  })
}

Upper piece of code, 2 main promises are promise1 and promise3. promise2 will be returned by promise3. I'm demonstating promise3 as a long promise chaning. To run, I initialized

上面的代码,2个主要承诺是promise1和promise3。 promise2将由promise3返回。我正在将promise3作为一个长期承诺来展示。为了运行,我初始化了

promise = [promise1(), promise3()];
Promise.all(promise).then(function(data){
  console.log('done', data);
})

Outcome was

结果是

promise1

promise1

promise2

promise2

promise3

promise3

done [ 'promise1', undefined ]

完成['promise1',undefined]

but I am expecting that

但我期待着这一点

done [ 'promise1', 'promise3' ]

完成['promise1','promise3']

My question is what is the best practice in this case?

我的问题是这种情况下的最佳做法是什么?

1 个解决方案

#1


3  

promise3 is missing a return. With this it works as expected.

promise3缺少回报。有了它,它按预期工作。

function promise3(){
  return promise2().then(function(){
  // ^^^ missing return here
    return new Promise(function(resolve, reject) {
      console.log('promise3');
      resolve('promise3')
    })
  })
}

Update:

更新:

If you simplify your case you're doing:

如果你简化你的案例你正在做:

var a = new Promise(function(resolve) {
  resolve("a");
});
var b = a.then(function () {
  return new Promise(function(resolve) {
    resolve("b");
  }));
});

And then your question is: "why is the resolved value of a not equal to b?". Well, they're two different promises.

然后你的问题是:“为什么解决的值不等于b?”。嗯,他们是两个不同的承诺。

With return a you return the original promise2. In a.then you eventually return promise3.

返回时,您将返回原始promise2。然后,你最终会返回promise3。

#1


3  

promise3 is missing a return. With this it works as expected.

promise3缺少回报。有了它,它按预期工作。

function promise3(){
  return promise2().then(function(){
  // ^^^ missing return here
    return new Promise(function(resolve, reject) {
      console.log('promise3');
      resolve('promise3')
    })
  })
}

Update:

更新:

If you simplify your case you're doing:

如果你简化你的案例你正在做:

var a = new Promise(function(resolve) {
  resolve("a");
});
var b = a.then(function () {
  return new Promise(function(resolve) {
    resolve("b");
  }));
});

And then your question is: "why is the resolved value of a not equal to b?". Well, they're two different promises.

然后你的问题是:“为什么解决的值不等于b?”。嗯,他们是两个不同的承诺。

With return a you return the original promise2. In a.then you eventually return promise3.

返回时,您将返回原始promise2。然后,你最终会返回promise3。