简体   繁体   English

在承诺链中容纳Q.all

[英]Accommodating a Q.all in a promise chain

I swear by Q for its simplicity, so I might not have done much research checking out other 'then' implementations. 我对Q的简单性起誓,因此我可能没有做太多研究来研究其他“ then”实现。 But I've used Q a good deal! 但是我已经大量使用了Q!

I have a chain of 'then' promises, and I want to resolve a 'batch' of promises in the middle of it and proceed with other operations in sequence; 我有一连串的“那么”的诺言,我想在其中解决“诺”的诺言,并按顺序进行其他操作。 so it's obvious I should use Q.all. 所以很明显我应该使用Q.all。 But I'm stuck here. 但是我被困在这里。 Either Q's doing it wrong, or I am. Q做错了,还是我做错了。

Here are two hypothetical async operations 这是两个假设的异步操作

var f=function(delay){
  return Q.delay(delay).then(function(){
    console.log("returning delayed",delay)
    return delay
  })
}

f2=function(delay){
  var defer=Q.defer()
  setTimeout(function(){
    console.log("returning timedout",delay)
    return delay
  },delay)
  return defer.promise
}

And this is the promise chain 这就是承诺链

  Q('begin')
    .then(console.log)
    .then(Q.all([100,200,300,400].map(f)))
    .then(function(){
      console.log("Finally",arguments)
    }).done()

And this is the output I wish 这是我希望的输出

begin
returning delayed 100
returning delayed 200
returning delayed 300
returning delayed 400
Finally { '0': undefined }

But this is the output I get 但这是我得到的输出

begin
Finally { '0': undefined }
returning delayed 100
returning delayed 200
returning delayed 300
returning delayed 400

I get the same sequence with f2 我得到与f2相同的序列

Now, if I run this 现在,如果我运行此

Q.all([100,200,300,400].map(f)).then(function(){
  console.log("Finally",arguments)
}).done()

I do get 我知道了

returning delayed 100
returning delayed 200
returning delayed 300
returning delayed 400
Finally { '0': [ 100, 200, 300, 400 ] }

but using f2 instead of f gives me 但是用f2代替f给了我

returning timedout 100
returning timedout 200
returning timedout 300
returning timedout 400

It doesn't execute the finally block. 它不执行finally块。

I get the same outputs with Q.[all/allResolved/allSettled] 我得到与Q相同的输出。[all / allResolved / allSettled]

So, my queries are, 所以,我的查询是

  1. How do I achieve the intended output by using Q.all specifically. 如何通过使用Q.all来达到预期的输出。 I do have a workaround though, but it doesn't look nice. 我确实有一种解决方法,但是看起来不太好。
  2. How are f and f2 different since the results by running Q.all().then() with them aren't the same. ff2有何不同,因为通过对它们运行Q.all()。then()的结果不同。

Q.all takes an array of promises, but then returns a promise (that is the only resolved when every promise in the array is resolved. Q.all接受一个Q.all数组,但随后返回一个promise(这是数组中每个promise解析后唯一解决的问题。

So, I think you need to return the result of Q.all in order to not immediately call the next then : 所以,我认为你需要返回的结果Q.all为了不立即打电话下一then

Q('begin')
    .then(console.log)
    .then(function() {
        return Q.all([100,200,300,400].map(f));
    })
    .then(function(){
      console.log("Finally",arguments)
    }).done()

Answering the second question, your usage of f2 is incorrect. 回答第二个问题,您对f2的使用不正确。

When you create a promise using a deferred you must resolve it in order for it to become fulfilled. 当您使用延期创建承诺时,必须解决该承诺才能兑现。 You can read more about it in How to convert a callback API to promises . 您可以在如何将回调API转换为promises中了解更多信息。

In your case you're creating an empty deferred and returning its promise but you never actually call .resolve on the deferred. 在您的情况下,您将创建一个空的递延并返回其诺言,但您从未真正对递延调用.resolve Doing it "more correctly" would be something like: “更正确地”执行此操作将类似于:

f2=function(delay){
  var defer=Q.defer()
  setTimeout(function(){
    console.log("returning timedout",delay)
    defer.resolve(delay); // note the resolve here vs the return
  },delay)
  return defer.promise
}

Using Q.delay works better since it's already promisified. 使用Q.delay效果更好,因为它已经实现了。 As for other then implementations Q is hardly bleeding edge right now to say the least :) 至于其他then性实现Q被几乎流血的边缘,现在至少可以说:)

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

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