简体   繁体   English

node.js + bluebird:解决promise.all

[英]node.js + bluebird: resolving promise.all

I got a function requestJSON that queries an external API and returns a (bluebird) promise. 我有一个函数requestJSON ,它查询一个外部API并返回一个(bluebird)承诺。

Problem: b gets resolved before c is added to the list so the promise.all runs while only a and b are resolved. 问题: b之前得到解决c被添加到列表,以便使promise.all运行,而只有ab得到解决。

Code: 码:

 let promises = []; // push promise a promises.push(requestJSON(..)); // push promise b promises.push(requestJSON(..).then((response) { // push promise c promises.push(requestJSON({foo: response.bar}); }); promises.all((data) => { console.log(data.length) // --> 2 }); 

Question: I couldn't come up with a really satisfying solution for this problem. 问题:对于这个问题,我无法提出真正令人满意的解决方案。 Is there a node-way / best-practice? 是否有节点方式/最佳实践?

Possible solutions 可能的解决方案

(I) Wrap b in another promise and resolve it inside c.then . (I)将b c.then另一个诺言,然后在c.then内部c.then
Problem: One extra promise that doesn't really do much except cluttering the code. 问题:除了使代码混乱之外,还有一个额外的承诺实际上并不会做很多事情。

 let promises = []; // push promise a promises.push(requestJSON(..)); // push helper promise promises.push(new Promise((resolve, reject) => { // push promise b promises.push(requestJSON(..).then((response) { // push promise c promises.push(requestJSON({foo: response.bar}); // resolve helper promise resolve(); }).catch(..); })); promises.all((data) => { console.log(data.length) // --> 4 }); 

(II) Put everything inside b.then . (II)将所有内容放入b.then
Problem: There's no semantic reason to place a and promise.all inside b + that solution reminds me of the pre-promise callback madness. 问题:有没有语义的理由把apromise.allb +该解决方案让我想起了,诺前期回调的疯狂。

 let promises = []; // push promise b promises.push(requestJSON(..).then((response) { // push promise a promises.push(requestJSON(..)); // push promise c promises.push(requestJSON({foo: response.bar}); promises.all((data) => { console.log(data.length) // --> 3 }); }); 

You must return something from a .then callback. 您必须从.then回调中返回一些内容。 Either a value (then the promise is considered resolved) or another promise, if you want to continue waiting for something else. 如果您想继续等待其他东西,则可以是一个值(然后该承诺被视为已解决)或另一个承诺。

let promises = [
    requestJSON(/* [1] */),
    requestJSON(/* [2] */).then(response => {
        return requestJSON(/* [3] */);
    })
];

// this waits for [1] and [3]
promises.all(promises).then(results => {
    console.log(data.length);
});

Your code did not return anything from the .then() . 您的代码未从.then()返回任何内容。 So the function return value is undefined and the promise gets settled with undefined as the result. 因此,函数的返回值是undefined ,并且promise将以undefined的结果作为结果。

The above can be written as (note the implicit return in arrow functions that don't have a full body): 上面的代码可以写成(请注意,没有完整正文的箭头函数的隐式return ):

let promises = [
    requestJSON(/* [1] */),
    requestJSON(/* [2] */).then(response => requestJSON(/* [3] */))
];

promises.all(promises).then(results => console.log(data.length));

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

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