If i am ina function which was called from inside another function, how do I exit out of the main/parent function?
e.g:
function(firstFunction(){
//stuff
secondFunction()
// stuff if second function doesnt exit
}
function secondFunction(){
if( // some stuff here to do checks...){
/***** Exit from this and firstFunction, i.e stop code after this function call from running ****/
}
}
The other answers are obviously correct, but I'd differ slightly and do it this way...
function firstFunction() {
if (secondFunction()) {
// do stuff here
}
}
function secondFunction() {
if (something) {
return false; // exit from here and do not continue execution of firstFunction
}
return true;
}
It's just a difference of opinion in coding styles really, and will have no difference to the end result.
You can return some value to indicate that you want to exit from the firstFunction().
e.g.
function(firstFunction(){
//stuff
rt = secondFunction()
if (rt == false) {
return; // exit out of function
}
// stuff if second function doesnt exit
}
function secondFunction(){
if( // some stuff here to do checks...){
/***** Exit from this and firstFunction, i.e stop code after this function call from running ****/
return false;
}
return true;
}
You cannot directly return control flow 2-steps up the stack. However you could return a value from your inner function which is then handled in the outer. Something like this:
function(firstFunction(){
var result = secondFunction()
if (!result)
return
}
function secondFunction(){
if( /* some stuff here to do checks */ ){
return false;
}
return true;
}
you should do a callback like this:
function firstFunction () {
secondFunction(function () {
// do stuff here if secondFunction is successfull
});
};
function secondFunction (cb) {
if (something) cb();
};
this way you can do asyncronous stuff in secondFunction too like ajax etc.
Related
I'd like to receive a response (true or false) from a called function to decide if the function should continue or stop. Look at the following code for better understanding:
function function1() {
function2(); // call function2
// after called function (here I need true or false, to decide if the function should stop or continue)
}
function function2() {
if (condition === value) {
// do something, give function1 a response to continue
} else {
// do something, give function1 a response to stop
}
}
Updated:
function function1() {
console.log('call function2');
function2(); // call function2
// after called function (here I need true or false, to decide if the function should stop or continue)
console.log('back from function2');
}
function function2() {
if (condition === false) {
console.log('condition === false');
return;
}
}
You don't need an else on the statement. check to see if your variable is false and if it is it will return if not the rest of your function will run automatically.
function function1() {
function2(); // call function2
// after called function (here I need true or false, to decide if the function should stop or continue)
}
function function2() {
if (condition === false) {
return;
}
}
If function2 is synchronous you can just return:
function function1() {
if(!function2()){
return
}; // call function2
// after called function (here I need true or false, to decide if the function should stop or continue)
}
function function2() {
if (condition === value) {
return true;
} else {
return false;
}
}
If function 2 does something asynchronous and expects a callback (one of the tags in your question) then it may be easier to write a function that will use function2 and returns a promise.
function function1(condition) {
console.log('calling function 2');
function2AsPromise(condition).then(function(
function2Result
) {
if (!function2Result) {
console.log('function 2 result is false');
return;
}
console.log('function 2 result is true');
});
console.log('exiting function 2');
}
function function2(condition, callback) {
setTimeout(function() {
if (condition) {
callback(true);
} else {
callback(false);
}
}, 2000);
}
function function2AsPromise(condition) {
return new Promise(function(resolve) {
function2(condition, resolve);
});
}
function1(false);
const function1 = check => {
if (check === false) {
return;
} else {
console.log("back from function2");
}
};
function1(false) // console.log doesn't run
function1(true) // console.log runs
make sure that you pass in a Boolean value.
Is possible to break execution program from function or I need to check boolean val returned?
Code
function check(something) {
if (!something) return;
// Else pass and program continuing
}
check(false); // I want to stop execution because function has returned
// Or I need to check value like if (!check(false)) return; ?
// I want easiest possible without re-check value of function..
alert("hello");
One way would be to through an Error, but otherwise you would need to use a boolean check, yes. I would recommend to use the boolean
function check(something) {
if (!something) throw "";
// Else pass and program continuing
}
check(false); // I want to stop execution because function has returned
// Or I need to check value like if (!check(false)) return; ?
// I want easiest possible without re-check value of function..
alert("hello");
Easiest...
(function(){
function check(something) {
if (!something) return false;
// Else pass and program continuing
}
if(!check(false)) return;
alert("hello");
});
(function(){ ... }); is called IIFE Immediately-invoked function expression.
Put your code in an IIFE, then you can use return
(function() {
function check(something) {
if (!something) {
return false;
} else {
return true;
}
}
if (!check(false)) {
return;
}
alert("hello");
});
I have two function like below:
var doSomething = function() {
// first check if user wants to proceed and some other restrictions
checkIfReallyShouldProceed();
console.log('ok, proceed');
// proceed and do something
}
var checkIfReallyShouldProceed = function() {
var check = confirm('really proceed?');
if(!check){
//stop executing doSomething
}
}
doSomething();
If the user does not confirm I want to return from doSomething. Of course I could return the result of the check variable to doSomething and have something like
if(!checkIfReallyShouldProceed()){
return;
}
there, but I want the called function to stop the calling function from executing. Is this possible and if so, how?
An if condition is made for this type of conditional procedure:
var doSomething = function() {
if (checkIfReallyShouldProceed()){
return true; // This will stop the doSomething function from executing
}
console.log('ok, proceed');
}
var checkIfReallyShouldProceed = function() {
return confirm('really proceed?'); // returns true/false
}
doSomething();
In the checkIfReallyShouldProceed function, return whether or not the user wants to proceed. In doSomething, it will stop executing if the called method returns true
Here in result() method, whenever it comes to else part, I need to get out of the function callthis().
It should not execute kumar() function.
Is this possible?
Actually, I can use like this
if(result) //if this method is true, it will come inside
{
kumar();
}
But this is not I want. while returning false from result() method, it should get out of the loop function calthis()
function calthis()
{
var i=1;
if(i==0)
{
alert("inside if");
}
else
{
alert("inside else");
result();
kumar();
}
}
function result()
{
var res = confirm("are you wish to continue");
if(res==true)
{
return true;
alert("inside if result");
}
else
{
return false;
}
}
function kumar()
{
alert("inside kumar");
}
Click here
There's a bunch wrong here.
First, if (result) just tests whether the variable result contains a truthy value, it doesn't actually invoke the function. If you want to test the return value of the function, you need
if (result()) {
Secondly, you're not understanding that return immediately leaves the current function. You can't meaningfully do this:
if(res==true)
{
return true;
alert("inside if result");
}
That alert cannot be reached. The function returns immediately when return true is encountered.
Thirdly, to exit callThis early, you simply need to return early. It's up to the function callThis to conditionally return; you cannot force a return from down inside the result function. A function cannot forcibly return out if the context that called it. It's not up to the internals of the result function to determine if kumar should run. You cannot influence the path of execution in the calling method directly. All result can do is return something, or (needlessly complex in this case) accept a callback and conditionally execute it.
Just return from callThis if the result of result() is false:
function calthis()
{
var i=1;
if(i==0)
{
alert("inside if");
}
else
{
alert("inside else");
if (!result()) return;
kumar();
}
}
To exit any function simply use return;
However it seems like you want to call a function if the user clicks confirm, is that correct? If so:
var res = confirm("are you wish to continue");
if (res === true)
{
kumar();
}
If you want to call a function if the user does not click confirm:
var res = confirm("are you wish to continue");
if (!res)
{
kumar();
}
You got a lot of confusing code going on there, but if the idea is to stop a function, simply use "return false;" and the code execution stops
How can I call the function only for once?
var myFunction = function () {
alert("calling function only for once");
}
myFunction();//alert "calling function only for once"
myFunction();//should not alert // if I call multiple times this should not be called
Try this:
var myFunction = function () {
alert("calling function only for once");
myFunction = function(){
return false;
}
}
myFunction();//alert "calling function only for once"
myFunction();//should not alert
Store some goobal variable a flag when run the function and check that variable at the start of the function.
set a flag, and call according to that flag:
var IsAlreadyCalled=false;
var myFunction = function () {
if(!IsAlreadyCalled){
alert("calling function only for once");
IsAlreadyCalled = true;
}
}
myFunction();//alert "calling function only for once"
myFunction();//should not alert
In your very odd scenario , the easiest way is to set a boolean:
var run = true,
myFunction = function(){
if(run){
alert('calling function only for once');
run = false;
} else {
return false;
}
};
myFunction(); // will run
myFunction(); // won't run
That way later on if you need to "reactivate" it you can just set the boolean back to true and call it again.
run = true;
myFunction(); // will run again
Other suggestions of using a flag are fine, but I would build it as a function decorator, that you can apply to any function. You avoid global variables this way, and your code becomes more readable and reusable:
// Takes a function and returns a function
// that executes only once
function once(f) {
var flag;
return function() {
if (!flag) {
flag = true;
return f.apply(this, arguments);
}
};
}
var fn = once(function() {
console.log('logged!');
});
fn(); // logged!
fn();
fn();
Demo: http://jsbin.com/povu/1/edit