繁体   English   中英

从 JavaScript 中的嵌套回调取消 function 执行

[英]Cancel function execution from nested callback in JavaScript

我遇到了以下问题:我正在执行一些嵌套回调(是的,我知道,也许不是最佳实践,但让我们假设重组会花费太多时间)并且有一种情况,我想停止执行来自这些回调之一的整个 function 。 它看起来像这样:

const myFunc = () => {
    func_1(data, (status) => {
        if (!status) {
            func_2(data, (status, result) => {
                if (status) {
                    if (result) {
                        func_3(data, result => {
                            return; // cancel execution and dont execute code after line 16
                        });
                    } else {
                        // do stuff, execute code after line 16     
                    }
                }
            });
        }
    });
    // code after
    var a = 2, b = 3;
    var c = a + b;
}

所以在 func_3 中有一个return子句,它应该返回 myFunc,意味着它应该停止执行。 不应执行“code after”注释之后的行。 在其他所有情况下,如果我不使用return进入这个分支,我想执行“之后的代码”。 我的问题是 return 确实取消了回调,而不是整个 function。 有没有可以让我得到我想要的结果的解决方案?

谢谢

罗伯特

您可以尝试以下方法

    const myFunc = () => {
    let stopAllFunction = false; // created a variable whose value can be over ridden from inner function.
    func_1(data, (status) => {
        if (!status) {
            func_2(data, (status, result) => {
                if (status) {
                    if (result) {
                        func_3(data, result => {
                            stopAllFunction = true;
                            return; // cancel execution and dont execute code after line 16
                        });
                    } else {
                        // do stuff, execute code after line 16     
                    }
                }
            });
        }
    });
    if (stopAllFunction) { // here we check to execute the below code or not
      return;
    }
    // code after
    var a = 2, b = 3;
    var c = a + b;
}

我创建了 stopAllFunction 变量,您可以在 function 内部对其进行编辑。

问题是你没有冒泡为什么较低的 function 终止了。
所以父 function 继续正常,因为它没有检查回调是如何终止的。

const myFunc = () => {
    func_1(data, (status) => {
        if (!status) {
            func_2(data, (status, result) => {
                if (status) {
                    if (result) {
                        // save the return
                        var ret = func_3(data, result => {
                            return -1; // cancel execution and dont execute code after line 16
                        });
                        // check why it terminated
                        if (ret === -1) {
                            // there was a error so I will also terminate
                            return;
                        }
                    } else {
                        // do stuff, execute code after line 16     
                    }
                }
            });
        }
    });
    // code after
    var a = 2, b = 3;
    var c = a + b;
}

现在我找到了一个可能不太漂亮的解决方案,但它正在完成工作。 我将回调包装在 promise 中,如果 promise 解析为 true,则执行第 16 行之后的代码。 如果它以 false 解决(发生什么而不是现在返回),则不会执行第 16 行之后的代码。

暂无
暂无

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

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