Let a function "return" a superfunction?

The code below is:

function two() {
    return "success";
}

function one() {
    two();
    return "fail";
}

If you check the code by calling the one () function, you always get "fail".

The question is, how can I return “success” in the one () function only by calling the two () function?

Is it possible?

Hi

+3
source share
4 answers

You cannot return a function from the function that called it in Javascript (or in many other languages, afaik). This requires one logic (). For instance:.

 function one() {
     return two() || "fail";
 }
+6
source
function one() {
   return two();
}
+6
source
function one() {    
    return two();
}
+2

, try-catch, , , , :

function two() {
  throw {isReturn : true, returnValue : "success"}
}


function one () {
  try {
    two()
  } catch(e) {
    if(e.isReturn) return e.returnValue;
  }
  return "fail";
 }

.

+2

Source: https://habr.com/ru/post/1769340/


All Articles