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;
}
2
  • Possible duplicate of How do I return the response from an asynchronous call? Commented Apr 4, 2017 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. Commented Apr 4, 2017 at 15:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

4 Comments

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).
Two things: 1. You changed the code after my comment. Your original code 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.
Based on my assumption that calling _myService.getData() is making asynchronous call to a web service..
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.