使用Promises纠正多路流的模式 [英] Correct pattern for multiway flows with Promises

查看:60
本文介绍了使用Promises纠正多路流的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我过去几天一直在玩承诺,只是尝试转换一些项目,使用承诺,但我已经多次修复这个问题。


阅读文章和教程时,一切看起来都很流畅:

So i have been playing with promises for the last few days, and just trying to convert some project, to use promises, but more than a few times i have encuntered this issue.

While reading articles and tutorials everything looks smooth and clean:

getDataFromDB()
.then(makeCalculatons)
.then(getDataFromDB)
.then(serveToClient)

但实际上,它不是像那样。
程序有很多if conditions改变整个流程:

But in reality, its not like that.
Programs have a lot of "if conditions" that changes the whole flow:

getDataFromCache(data).then(function(result){
    if(result){
        return result;
    }else{
        return getDataFromDB();
    }
}).then(function(result){
    if(result){
        serveToClient() //this does not return a promise, so undefined returned...
    }else{
        return getDataFromWebService(); //this does return a promise, 
    }
}).then(function(result){
    //i dont want to reach here if i already serveToClient()...
    //so i basically have to check "if(result)" for all next thens
    if(result){
       //do more stuff
    }
}).then(...

我有两个主要问题:


  1. 我发现自己在上添加了大量的条件然后回调。

  2. 我仍然进入下一个然后 callbac ks,即使我已经完成了( serveToClient

  1. I find myself adding alot of if conditions on the then callbacks.
  2. I am still getting into the next then callbacks, even if i already finished (serveToClient)


Am我遵循正确的模式?


Am i following the correct pattern?

推荐答案

你无法避免如果语句,因为逻辑流程需要这些语句。如果您不希望在的一部分中继续保证链,则必须分支您的控制流。因此,如果你的第二个 .then()处理程序的某些部分,你不想继续第三个 .then() handler,然后你需要像这样分支逻辑并在第二个 .then()中放入后续的 .then()处理程序处理程序在自己的逻辑分支中。

You can't avoid the if statements since that is required for your logic flow. You will have to branch your flow of control if you don't want to continue the promise chain in one part of the if. So if in some part of your second .then() handler, you don't want to go on to the third .then() handler, then you need to branch the logic like this and put subsequent .then() handlers inside the 2nd .then() handler in their own branch of the logic.

你不能只是继续顶级分支,因为中止未来 .then()逻辑的唯一方法在主链中要么拒绝承诺(你可能不想做)或者添加另一个如果检入每个 .then( )处理程序来决定是否应该跳过(yuck)。

You can't just continue the top level branch because the only way to abort future .then() logic in the main chain is to either reject the promise (which you probably don't want to do) or add another if check in every .then() handler to decide whether it should be skipped or not (yuck).

所以相反,你可以像这样分支逻辑:

So instead, you can branch the logic like this:

getDataFromCache().then(function(result){
    if(!result) {
        return getDataFromDB()
    } else {
        return result;
    }
}).then(function(result){
    // branch promise chain here into two separate branches
    if(result){
        // do not continue the promise chain here
        // call a synchronous operation
        serveToClient();
    } else {
        // continue promise chain here
        return getDataFromWebService().then(function(result) {
            if(result){
               //do more stuff
            }
        }).then(...);    // you can continue the promise chain here
    }
}).catch(function(err) {
    // process any errors here
});

您可能会发现这些其他答案很有用:

You may find these other answers useful:

了解javascript承诺;堆叠和链接

promise.then.then vs promise.then之间是否有区别; promise.then

仅供参考,你可以重新组织上面的代码,使其更加简洁如下:

FYI, you can reorganize the above code to be a bit more concise like this:

getDataFromCache().then(function(result) {
    if (result)
        serveToClient();
    } else {
        return getDataFromWebService().then(function(result) {
            if(result){
               //do more stuff
            }
        }).then(...);    // you can continue the promise chain here
    }
}).catch(function(err) {
    // process any errors here
});

这篇关于使用Promises纠正多路流的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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