I have scenario where I want to break the next function call if the previous function has return statement but I can see it is executing next function call even after I have return statement in below function named "b".
function main(){
a()
b();
c();
}
function a(){
console.log("function a call!!");
}
function b(){
console.log("function b call!!");
return function(){return 0;}
}
function c(){
console.log("function c call!!");
}
main()
Outout :
'function a call!!'
'function b call!!'
'function c call!!'
Expected output:
'function a call!!'
'function b call!!'
Can someone can explain me what is right way to do here?
You cannot do control flow on a caller function from a callee function. your code just returns a function
from b()
, but that function is never executed, and even if it is, it won't have the expected behavior. If you want to interrupt main
, have to do it in main
itself:
function main(){
a()
const bReturnValue = b();
if(bReturnValue <some condition>) {
return;
}
c();
}
function a(){
console.log("function a call!!");
}
function b(){
console.log("function b call!!");
return function(){return 0;}
}
function c(){
console.log("function c call!!");
}
main()
You can't cancel the function call of C since you don't check for what b returns. Let b return true for example, and check with
let resultB = b();
if(!resultB) c();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.