避免使用承诺的嵌套回调 [英] avoiding nested callbacks with promises

查看:43
本文介绍了避免使用承诺的嵌套回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用Promise API还是很陌生,我在努力避免深层嵌套的Promise链,据我所知,这是使用Promises的好处之一.以下面的伪代码为例,当后续的依赖于先前的上下文时,如何避免嵌套Promises?

I'm still new to using Promise APIs and I'm struggling how to avoid deeply nested Promise chains, which as far as I understand, are one of the benefits to using Promises. Using the following pseudo code as an example, how do you avoid nesting Promises when the subsequent ones rely on the context of prior ones?

function loadDependency1() {
    // return a promsise to load the first dependency
}

function loadDependency2(dependency1) {
    // return a promise to load the second dependency, which relies on the first dependency
}

function loadDependency3(dependency2) {
    // return a promise to load the third dependency, which relies on the second dependency
}

function doWork(dependency1, dependency2, dependency3) {
    // finally have all the things necessary to do work
}

// load all the dependencies and eventually doWork
loadDependency1().then(function(dependency1) {
    return loadDependency2(dependency1).then(function(dependency2) {
        return loadDependency3(dependency2).then(function(dependency3) {
            doWork(dependency1, dependency2, dependency3);
        });
    });
});

推荐答案

当您从 then 返回诺言时,当诺言得到解决时,它将解决 :

When you return a promise from then, it will resolve when that promise resolves:

因此,如果下一个只需要上一个:

So, if the next one only needs the previous one:

loadDependency1().then(function(dependency1) {
    return loadDependency2(dependency1);
}).then(function(dependency2) {
     return loadDependency3(dependency2);
}).then(function(dependency3) {
    doWork(dependency3);
});

如果需要第三个依赖项,则可以使用.

Works if you need the third dependency.

如果依赖关系不相互依赖:

If the dependencies are not dependent of each other:

Promise.all([loadDependency1(),loadDependency2(),loadDependency3])
.spread(function(dep1,dep2,dep3){
    doWork(dep1,dep2,dep3);
});

如果您想在承诺链中保持状态",并使用诸如 Bluebird 之类的现代承诺库,则可以:

If you want to keep 'state' across the promise chain and are using a modern promise library such as Bluebird you can do:

loadDependency1().bind({}).then(function(dependency1) {
    this.dep1 = dependency1;
    return loadDependency2(dependency1);
}).then(function(dependency2) {
     this.dep2 = dependency2;
     return loadDependency3(dependency2);
}).then(function(dependency3) {
    doWork(this.dep1, this.dep2, dependency3);
});

如果您不是(而且您确实应该是:)),您可以 .all 绕过它:

If you're not (and you really should be :) ) You can .all you way around it:

loadDependency1().then(function(dependency1) {
    return [loadDependency2(dependency1),dependency1];
}).spread(function(dependency2,dep1) {
     return [loadDependency3(dependency2),dependency2,dep1];
}).spread(function(dependency3,dependency2,dependency1) {
    doWork(dependency1, dependency2, dependency3);
});

这篇关于避免使用承诺的嵌套回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆