Home > Blockchain >  Cancel function exection from inside a callback in JavaScript
Cancel function exection from inside a callback in JavaScript

Time:07-27

I have got the following problem: I am executing some nested callbacks (yes I know, maybe not best practice, but let us just assume restructuring would take too much time) and there is one case, in which I want to stop the execution of the entire function from inside one of those callbacks. It looks like this:

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;
}

So in func_3, there is a return-clause, which should return myFunc, means, it should stop its execution. The Lines after the "code after"-comment should not be executed. In every other case, where I do not enter this branch with the return, I want to execute the "code after". My problem is that the return does cancel the callback only, not the entire function. Is there a solution which would get me the result I want?

Thank you

Robert

CodePudding user response:

What you can try below approach

    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;
}

I have create stopAllFunction variable which you can edit inside of the function.

CodePudding user response:

The problem is that you are not bubbling up why the lower function terminated.
So the parent function continues normally because it is not checking how the callback terminated.

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;
}
  • Related