0

So I have this javascript code. There are 2 nested functions. How can I return a value to funcA() inside funcB() ?

function funcA() {
   funcB(() => {
        //return funcA inside here 
    })
}

Is this even possible without doing it like the following?

function funcA() {
    let returnValueA;
    funcB(() => {
        //change returnValueA inside here
    })
    return returnValueA;
}

Behemoth
  • 5,389
  • 4
  • 16
  • 40
  • 1
    in your example, `returnValueB` is not defined, and `funcB` is never called and `returnValueA` is never used ... so .. the answer is a pineapple – Jaromanda X Aug 06 '20 at 09:12
  • I think the keyword is "recursion" – agentp Aug 06 '20 at 09:15
  • @agentp — No. That would be if `funcA` conditionally called *itself*. – Quentin Aug 06 '20 at 09:16
  • It depends on what `funcB` does. If it's a *synchronous* callback, there may be things you can do and/or `funcB` should handle the return value appropriately. If it's an *asynchronous* callback, there's no way. – deceze Aug 06 '20 at 09:16
  • @Quentin Indeed, the question has been thoroughly edeted multiple times. But it's still [possible](https://jsfiddle.net/br6jxgu0/1/), `return funcB(...)`, if `funcB` is synchronous, and returns a value created by the callback. – Teemu Aug 06 '20 at 09:24
  • For some reason I can't answer. Check out generators: [mdn](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Generator) – Nathan Chappell Aug 06 '20 at 09:28
  • Thanks for the help to y'all! I have made a few typing mistakes when publishing the question. Sorry if the edits were confusing :) – Behemoth Aug 06 '20 at 11:57

1 Answers1

1

In short: No.

A return statement only defines what is returned from the function it is part of.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335