javascript 当一个 promise 依赖于另一个 promise 时,Bluebird 的 Promise.all() 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25895562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 05:11:47  来源:igfitidea点击:

Bluebird's Promise.all() method when one promise is dependent on another

javascriptpromisebluebird

提问by Jeff

I'm writing some code that currently looks like this because I have dependencies in my code. I was wondering if there was a cleaner way to do this with Promise.all()? Here is my pseudo code:

我正在编写一些当前看起来像这样的代码,因为我的代码中有依赖项。我想知道 Promise.all() 是否有更简洁的方法来做到这一点?这是我的伪代码:

        return someService.getUsername()
            .then(function(username) {
                user = username;
            })
            .then(function() {
                return someService.getUserProps(user);
            })
            .then(function(userProps) {
                userProperties = userProps;
                return someService.getUserFriends(user);
            })
            .then(function(userFriends) {
                friends = userFriends;
            })
            .catch(error)
            .finally(function(){
                // do stuff with results
            });

The important thing is that I need user before I can make the second two callsfor getUserProps() and getUserFriends(). I thought I could use Promise.all() for this like so:

重要的是我需要用户才能对 getUserProps() 和 getUserFriends() 进行后两个调用。我想我可以像这样使用 Promise.all() :

var user = someService.getUsername()
    .then(function(username) {
        user = username;
    })
var getUserProps = someService.getUserProps(user);
var getUserProps = someService.getUserFriends(user);

return Promise.all(user, getUserProps, getUserFriends, function(user, props, friends) {
    // do stuff with results
})

But I cannot get this to work. Is this the correct case to use .all?

但我无法让它发挥作用。这是使用 .all 的正确情况吗?

回答by jfriend00

Promise.all()is designed for parallel operation where you launch a bunch of async operations to run at the same time and then it tells you when they are all done.

Promise.all()专为并行操作而设计,您可以在其中启动一堆异步操作以同时运行,然后它会告诉您它们何时全部完成。

It does not sequence one versus the completion of another in any way. So, you can't use it to wait for the user to be ready and then have the other operations use that user. It just isn't designed to do that.

它不会以任何方式对一个与另一个的完成进行排序。因此,您不能使用它来等待用户准备就绪,然后让其他操作使用该用户。它只是不是为了做到这一点而设计的。

You could get the user first and then when that is complete, you could use Promise.all()with your other two operations which I think can be run at the same time and don't depend upon each other.

您可以先获取用户,然后在完成后,您可以Promise.all()将其他两个操作与我认为可以同时运行且互不依赖的操作一起使用。

var user;
someService.getUsername().then(function(username) {
    user = username;
    return Promise.all(getUserProps(user), getUserFriends(user));
}).then(function() {
    // do stuff with results array
}).catch(function() {
    // handle errors
});

回答by Benjamin Gruenbaum

You can use .allbut you're going to have to make sure they run sequentially your code, you can do this by .thening them like you've done. If you do that you should use .joinwhich is a shorthand for .all([...]).spread(....

您可以使用,.all但您必须确保它们按顺序运行您的代码,您可以通过.then像您所做的那样对它们执行此操作。如果你这样做,你应该使用.joinwhich 是.all([...]).spread(....

var user = someService.getUsername();
var props = user.then(getUserProps)
var friends = user.then(getUserFriends)
Promise.join(user, props, friends, function(user, props, friends) {

    // everything is available here, everything is synchronized
});

If what you were trying to solve is the closure/nesting issue - then this is the way to do so.

如果您试图解决的是关闭/嵌套问题 - 那么这就是这样做的方法。

回答by talal7860

Promise.all() is a way to execute a list of promises in parallel but if we want to execute a list of promises in a series where one is dependent on the other, we've to solve it a bit differently

Promise.all() 是一种并行执行 Promise 列表的方法,但是如果我们想在一个依赖于另一个的系列中执行一个 Promise 列表,我们必须以不同的方式解决它

// Promise returning functions to execute
function doFirstThing(){ return Promise.resolve(1); }  
function doSecondThing(res){ return Promise.resolve(res + 1); }  
function doThirdThing(res){ return Promise.resolve(res + 2); }  
function lastThing(res){ console.log("result:", res); }

var fnlist = [ doFirstThing, doSecondThing, doThirdThing, lastThing];

// Execute a list of Promise return functions in series
function pseries(list) {  
  var p = Promise.resolve();
  return list.reduce(function(pacc, fn) {
    return pacc = pacc.then(fn);
  }, p);
}

pseries(fnlist);  
// result: 4