0

Why am i getting the following error:ReferenceError: result is not defined"

function returnData() {        
  _myService.getData().then(function(data) {
    var result = data;
  });
  return result;
}
Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
1future
  • 255
  • 5
  • 21
  • Possible duplicate of [How do I return the response from an asynchronous call?](http://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – nvioli Apr 04 '17 at 15:51
  • @nvioli: It's certainly **very** closely related, but the actual error the OP is asking about is a scope issue instead. But again, **very** closely related. – T.J. Crowder Apr 04 '17 at 15:55

3 Answers3

2

Because result is declared within the callback you passed into then. It doesn't exist outside.

You can declare it outside, but note that it won't have the data as of your return result; line later:

function returnData(){
    var result;           // <==== Note

    _myService.getData().then(function(data){

      result = data;      // <==== No `var` here
    })

    // WON'T HAVE THE VALUE HERE, it's too soon
    return result;
}

See How do I return the response from an asynchronous call? for why, and what to do about it. Basically, you don't need the returnData function at all, it doesn't add any value (other than perhaps encapsulation around _myService). It cannot return the value. So either use _myService directly, or if you're trying to hide that, just

function returnData() {
    return _myService.getData();
}

...and use then in the calling code to receive it. Since again, returnData can't return the data.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
0

I suppose that calling _myService.getData() you are making an Asynchronous call to a web service..

In that case you can return your service response data like this:

function returnData() {
  return _myService
    .getData()
    .then(function(data) {
      // Here you can work with the response data
      return data;
    });
}

Notice that you can work with the response in the callback, as you correctly passed into then.

Yosvel Quintero
  • 18,669
  • 5
  • 37
  • 46
  • Why? What's different? Why does it matter? A good answer should *explain* things. Separately: There's zero purpose to that `then` handler. It should just be `function returnData() { return _myService.getData(); }` if it's going to exist at all (as I said in my answer). – T.J. Crowder Apr 04 '17 at 15:55
  • Two things: 1. You changed the code after my comment. Your [original code](http://stackoverflow.com/revisions/43212031/1) had `.then(function(data) { return data; })` which is the completely pointless thing I was referring to. 2. The new code is wrong, as far as we can tell from the question. There's no indication in the question that the value received by the callback has a `data` property or that the OP wants that instead of the entire value. It's not better or cleaner. Sometimes we might well need some kind of transform like that, but we have no reason to think we do here. – T.J. Crowder Apr 04 '17 at 16:28
  • Based on my assumption that calling `_myService.getData()` is making asynchronous call to a web service.. – Yosvel Quintero Apr 04 '17 at 16:30
  • Oh, I don't think that's an assumption, we can be pretty sure it's asynchronous (and the callback to `then` is guaranteed to be). But it's also irrelevant. The shape of what's passed to the callback has nothing whatsoever to do with whether it's synchronous or asynchronous. It has to do with what `getData`'s promise resolution value is -- which isn't specified, but what we can see from the code in the question is that they want the whole thing, not just a part of it. – T.J. Crowder Apr 04 '17 at 16:32
0

@Yosvel's answer is correct, the reason is because you are making an Asynchronous call to your service.

In doing so, you must wait for a result to be returned to you, which can then be processed in your then function.

By having two return calls, your return value is undefined because it is not waiting for the callback function, but simply returning a null value.