0

I wish to be able to return and end a function from inside a function inside that function. Just typing return inside the function will return for that function instead of the one I'm trying to return for.

function iWannaEscapeThis() {
     function someSideFunction() {
         //need to return iWannaEscapeThis here somehow
    }
}
Nino Filiu
  • 16,660
  • 11
  • 54
  • 84
  • read basics of JS, ES and etc. and develop logical thinking abilities. it's commons, if You cannot think about such simple things, so I don't think that You'll be good developer. – num8er Mar 18 '19 at 23:39
  • Do you need to return iwannaescapethis, or you want to end iwannaescapethis function? – croraf Mar 18 '19 at 23:42
  • 1
    The answer is either "it's impossible" or a simple `return` statement. It really depends on how `somesidefunction` is called. Maybe a duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/q/14220321/218196). – Felix Kling Mar 18 '19 at 23:42
  • If you want this to be a generic escape mechanism were you could return from any sub function within `iWannaEscapeThis`, you could use a custom Exception and trap for this. – Keith Mar 18 '19 at 23:50

3 Answers3

3

You need to call it main function and return it

function iwannaescapethis() {
     function somesidefunction() {
         return 'from inside'
    }
    return somesidefunction();
}
console.log(iwannaescapethis())

Async function

async function iwannaescapethis() {
     async function somesidefunction() {
         let promise = new Promise((resolve, reject) => {
            setTimeout(() => resolve("from inner"), 2000)
         });
         return await promise;
    }
    let x = await somesidefunction()
    return x;
}
(async function(){
  let res = await iwannaescapethis()
  console.log(res)
})()
Maheer Ali
  • 35,834
  • 5
  • 42
  • 73
  • Remove async, await words from second function and simply `return new Promise...`. Cause async/await is syntactic sugar on promises. So when Your method returns promise it means that method can be await-ed – num8er Mar 19 '19 at 00:17
  • @num8er I can't get what you mean. I will be thankful if you give me the correct version of my code – Maheer Ali Mar 19 '19 at 00:23
  • @num8er So what!? Why should he remove it? It does the same. – Amiratak88 Mar 19 '19 at 00:51
  • @Amiratak88 I did not say that he is wrong. He asked me to review his answer. So my opinion is that: It does the same, but it wraps already written promise inside of unnecessary extra promise. in fact there is just 1 promise inside `somesidefunction`, wrapping in extra async/await implicitly puts it inside another promise. If You don't like it write You own answer. – num8er Mar 19 '19 at 09:20
0

Return the nested function:

function iwannaescapethis() {
  function somesidefunction() {
    return "sideFunction";
  }
  return somesidefunction();
}
console.log(iwannaescapethis());
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

It's possible to use exceptions for doing this, but a big warning it's not advised you use exceptions for flow control too often.

But if you really do need this feature, here is an example.

class ReturnException extends Error {
  constructor (value) { super(); this.value = value; }
};


function thisIsTheOuterFunction() {
  try {
    function someSubFunction() {
      function someOtherSubFunction() {
        throw new ReturnException("The answer is 42");
      }
      someOtherSubFunction();
    }
    someSubFunction();
  } catch (e) {
    if (e instanceof ReturnException) {
      return e.value;
    } else throw e;
  }
}

console.log(thisIsTheOuterFunction());
Keith
  • 22,005
  • 2
  • 27
  • 44