简体   繁体   English

Q.all不工作

[英]Q.all is not working

I have a problem with my node.js e2e test. 我的node.js e2e测试有问题。 I want to wait for 2 promises to resolve. 我要等待2个诺言解决。 But for some reason when I use Q.all it is just freezing my app. 但是由于某种原因,当我使用Q.all时,它只是冻结了我的应用程序。 I am using kriskowal's Q 1.0.0. 我正在使用kriskowal的Q 1.0.0。

This works: 这有效:

var doStuff = function() {
    var promiseA = getPromiseA();
    return promiseA;
}


var prepareTestData = function(done) {
    doSomething()
    .then(doStuff)
    .then(done);
}

But this does not: 但这不是:

var doStuff = function() {
    var promiseA = getPromiseA();
    var promiseB = getPromiseB();
    return [promiseA, promiseB];
}


var prepareTestData = function(done) {
    doSomething()
    .all(doStuff)
    .then(done);
}

Can someone help me out what I am missing? 有人可以帮我解决我所缺少的吗?

* Updated * * 更新 *

The simplified answer to your question is that the all() prototype method doesn't take parameters, so .all(doStuff) is only calling .all() on the promise returned by doSomething() and doStuff ends up being an argument that's never used. 您问题的简化答案是all()原型方法不带参数,因此.all(doStuff)仅对doSomething()返回的promise调用.all(),而doStuff最终成为一个从不接受的参数用过的。 The simplest solution is to use nikc.org's solution. 最简单的解决方案是使用nikc.org的解决方案。

There are two ways to use .all() in Q. 在Q中使用.all()有两种方法。

One is to use Q.all() on an array of promises to create a promise whose result is an array of all the resolutions of those promises. 一种是对一个promise数组使用Q.all()来创建一个promise,其结果是这些promise的所有分辨率的数组。

var doStuff = function() {
    var promiseA = getPromiseA();
    var promiseB = getPromiseB();
    return Q.all([promiseA, promiseB]);
}


var prepareTestData = function(done) {
    doSomething()
    .then(doStuff)     // use then here
    .then(done);
}

The other (as shown in nikc.org's answer) is to call .all() on a promise whose result is an array of promises. 另一个(如nikc.org的答案所示)是在一个promise上调用.all() ,其结果是一个promise数组。 This will produce a new promise whose result is an array of the resolutions of all those promises: 这将产生一个新的诺言,其结果是所有这些诺言的解决方案的数组:

var doStuff = function() {
    var promiseA = getPromiseA();
    var promiseB = getPromiseB();
    return [promiseA, promiseB];
}


var prepareTestData = function(done) {
    doSomething()
    .then(doStuff)
    .all()
    .then(done);
}

In both cases, the result passsed to done will be an array with the resolved values of promiseA and promiseB . 在这两种情况下,传递给done的结果都是一个具有promiseApromiseB解析值的promiseB

The call to Promise.all should be after calling doStuff which is returning the array of promises. Promise.all的调用应该在调用doStuff ,该doStuff返回了promise数组。 Alternatively, you return Q.all(Array) from doStuff . 或者,您从doStuff return Q.all(Array)

var prepareTestData = function(done) {
    doSomething()
    .then(doStuff) // array returned here
    .all()         // all creates a promise of promises
    .then(done);
}

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

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