1

First, have a look at this sample:

var calculate = function(callback) {
  // calculating...
  callback(5);
}

var parent = function() {
  var x = 0;

  var calculatedValue = calculate(function(output) {
    return output; // return #1

    x = output;
  });

  return x; // return #2

  return calculatedValue; // return #3
}

My objective is to "calculate" a value within the calculate function, and this value must be returned by the parent function.

Where should I place the return statement, and what should I return with it, in order to achieve this?

I tried 3 cases:

return #1 Here the output value is returned by the callback function, and not the parent function, so parent returns undefined.

return #2 Here the value of x is returned before executing the callback function, so parent returns 0.

return #3 + return #1 Here, I tried to return the output from the callback, and then (thinking that the return value of callback may somehow become the return value of the calculate itself), returned the calculatedValue, but again, parent returns undefined.

Please suggest a way, and plunk's over here

Anubhav Dhawan
  • 1,431
  • 6
  • 19
  • 35
  • Is `calculating` asynchronous? Can you show us your actual code, please? – Bergi Sep 02 '16 at 07:03
  • I hope not. See [here](http://plnkr.co/edit/WdF8TzxPUPcIZDIVY94O?p=preview). There's no actual code, it's just a thought. No worries :) – Anubhav Dhawan Sep 02 '16 at 07:08
  • 1
    If that is really all your code, then it's synchronous (I had expected there to be something more behing `// calculating...`). So why are you using a callback at all instead of simply doing `return 5`? – Bergi Sep 02 '16 at 07:27
  • @Bergi Actually, the calculate function is supposed to be some predefined library function (I mocked it up here). So, the real question here is, (synchronous or not) how to return the value of a library function, which returns values through a callback function, like calculate does? – Anubhav Dhawan Sep 02 '16 at 07:50
  • Libraries that take callbacks are typically asynchronous, and [you cannot `return`](http://stackoverflow.com/q/14220321/1048572). Use callbacks. – Bergi Sep 02 '16 at 08:23

1 Answers1

4

In this particular case, your callback is synchronous, which means you can return from it like so:

var calculate = function(callback) {
  // calculating...
  return callback(5);
}

Then, you can return from the parent like so:

var parent = function() {    
  var calculatedValue = calculate(function(output) {
    return output; // tells the callback to do nothing with the output, just return it.
  });

  return calculatedValue; // return from parent
}

Note that if calculate is an asynchronous function, this approach won't work. Have a look at How do I return the response from an asynchronous call? for that.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Ok, I tried this [here](http://plnkr.co/edit/WdF8TzxPUPcIZDIVY94O?p=preview), but still got `undefined`. I hope my callback is synchronous. – Anubhav Dhawan Sep 02 '16 at 06:59