Given is the following code:
function two() {
return "success";
}
function one() {
two();
return "fail";
}
If you test the code by calling function one(), you will always get "fail".
The question is, how can I return "success" in function one() by only calling function two()?
Is that even possible?
Regards
You can't make a function return from the function that called it in Javascript (or many other languages, afaik).
You need logic in one() to do it. E.g.:
function one() {
return two() || "fail";
}
function one() {
return two();
}
You could do it, using a try-catch Block, if your function one anticipates a probable non-local-return as well as function two like this using exceptions:
function two() {
throw {isReturn : true, returnValue : "success"}
}
function one () {
try {
two()
} catch(e) {
if(e.isReturn) return e.returnValue;
}
return "fail";
}
, I believe.
function one() {
return two();
}
Related
I'd like to stop returning to the first function. Look at my example for better understanding:
function function1() {
function2();
console.log('back after called function'); // shoudn't log if condition is true
}
function function2() {
if (condition === true) { // if condition is true, stop right here (no callback to function1)
return false;
} else {
// callback is allowed
}
}
Use a ternary operator.
function2() ? false : console.log('back after called function');
You can add more lines in as well - use an IIFE.
function2() ? false : (() => {
console.log('back after called function');
//More code
})();
I think it is impossible for function2 not to return to function1
function function1() {
if (function2()) return;
console.log('back after called function'); // shoudn't log if condition is true
}
Simple Just throw and caught the error
function function1() {
try{
function2();
console.log('back after called function'); // shoudn't log if condition is true
}catch(e){
console.log(e.message);
}
}
function function2() {
if (1 === 1) { // if condition is true, stop right here (no callback to function1)
throw new Error('forced stopped');
} else {
// callback is allowed
}
}
<!DOCTYPE html>
<html>
<body onLoad="function1()">
</body>
</html>
The simplest way should be something like:
function function1() {
if (function2()) {
console.log('back after called function');
}
}
As Jack Bashford suggested (but wrote in the wrong way), you could use the ternary operator:
function function1() {
function2()? console.log('back after called function') : return;
}
Or, for a more complex code process, you could use a callback function:
function function1() {
function2(function(error) {
if (!error) {
console.log('back after called function');
}
});
}
function function2(callback) {
if (condition === true) {
callback("Error: condition false");
} else {
callback(null);
}
}
EDIT:
Using my last option you can do exactly what you asked: do not return to function1:
function function1() {
function2(function() {
console.log('back after called function');
});
}
function function2(callback) {
if (condition === true) {
return;
} else {
callback();
}
}
But I think for the sake of good practice you should always call the callback.
Here is my code i want alert to give (some data) value. which is of inner function it give undefined value
alert(f1());
function f1(){
f2(function (){
return "some data";
});
}
function f2(f4){
// some code
}
visit : https://jsfiddle.net/dvdhyttr/8/
i want alert get value (some data).
but getting undefined.
Two main issues
Missing return within the function f1
Within function f2 you need to return the result of calling the function f4.
alert(f1());
function f1() {
return f2(function() {
return "some data";
});
}
function f2(f4) {
return f4()
}
You need to assign the return values of the functions to variables in the outer function and return them, all the way up to alert(f1());.
alert(f1());
function f1() {
const resultOfF2 = f2(function() {
return "some data";
});
return resultOfF2;
}
function f2(f4) {
return f4();
// some code
}
Following is my pseudo code:
function main() {
return one() || two();
}
function one() {
console.log("one");
}
function two() {
console.log("two");
}
main();
I recently studied that in the return statement if one and two functions are variables containing any values then one is returned unless it is false or null or undefined but what about while using functions in this example?
can someone help me...
1) You're not returning any values. You're writing to the console so what is returned is undefined
2) undefined is false so the || evaluates both sides.
The return value of your one() and two() functions are undefined, as those are just logging the strings to the console, but not returning those.
Try this:
function main() {
return one() || two();
}
function one() {
console.log("one");
return "one";
}
function two() {
console.log("two");
return "two";
}
main();
The "value" of both the one and two function calls are the undefined value, because they do not return any other value.
So it would seem that you're really asking why both functions are printing to the console. This is because the act of printing to the console does not establish the "truthy" or "falsey" state of the functions. This is determined only by the function calls' return value, as noted above.
I'm working on a big project and I simplified what it matters here. This is the code:
a = new Thing(/*sayHi + sayHey*/);
function sayHi() {
alert("hi");
}
function sayHey() {
alert("hey");
}
function Thing (callback) {
callback();
}
I'd like to, with just the callback parameter, call both the sayHi() and the sayHey() function, at the order I put them. Is it possible? How would I do it? Thank you.
Pass an anonymous function that calls both of them sequentially:
a = new Thing(function() {
sayHi();
sayHey();
});
function sayHi() {
alert("hi");
}
function sayHey() {
alert("hey");
}
function Thing (callback) {
callback();
}
Alternatively to #Barnar's answer, create and pass a regular named function. If the callback logic gets heavier, you might want that anyway.
function hiHeyCallback() {
sayHi();
sayHey();
}
a = new Thing(hiHeyCallback);
In Javascript, is there some way to bind and event handler of one function activating and ending?
So, for instance, I have two functions:
function one() { console.log("this is function one") }
and
function two() { console.log("this is function two") }
I want function two to activate both when function one is called and when it ends. Obviously, I could just:
function one() { two(); console.log("this is function one"); two() }
but that'd be boring -- not nearly as interesting as this way.
Well, you could write a function that wraps the original function in another that calls the callback.
function bindStartEnd(originalFn, callback, thisArg) {
return function() {
var returnValue;
callback();
returnValue = originalFn.apply(thisArg || null, arguments);
callback();
return returnValue;
};
}
It could be used like this:
function one() {
console.log("This is function one");
}
function two() {
console.log("This is function two");
}
var three = bindStartEnd(one, two);
three();
And it could be extended to also accept two callbacks, one for the beginning and one for the end. You might also think of a better name.