简体   繁体   English

如何停止使用async-await执行下一个功能?

[英]How to stop executing next function with async-await?

I'm using this library to chain asynchronous functions in my nodejs app: https://github.com/yortus/asyncawait 我正在使用这个库在我的nodejs应用程序中链接异步函数: https : //github.com/yortus/asyncawait

var chain = async(function(){

    var foo = await(bar());
    var foo2 = await(bar2());
    var foo3 = await(bar2());

}

So bar3 waits for bar2 to finish and bar2 waits for bar() to finish. 因此,bar3等待bar2完成,而bar2等待bar()完成。 That's fine. 没关系。 But what will I do in order to stop the async block from further execution? 但是我该怎么做才能阻止异步块进一步执行? I mean something like this: 我的意思是这样的:

var chain = async(function(){

    var foo = await(bar());
    if(!foo){return false;} // if bar returned false, quit the async block
    var foo2 = await(bar2());
    var foo3 = await(bar2());

}

what's the best approach to handle this? 处理此问题的最佳方法是什么?

at the moment I throw an exception within bar and handle the exception in way: 目前,我在bar中抛出异常并以以下方式处理该异常:

chain().catch(function (err) { //handler, ie log message)

It's working, but it doesn't look right 它正在工作,但看起来不正确

I mean something like this … 我的意思是这样的……

asyncawait supports exactly this syntax. asyncawait 完全支持此语法。 Just return from the function: 只需从函数return

var chain = async(function(){
    var foo = await(bar());
    if (!foo) return;
    var foo2 = await(bar2());
    var foo3 = await(bar2());
});

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

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