0

I'm trying to call service within a function and then to use response data from the service, using .then, but I'm not able to return anything from that function

 var status;

 var getStatus = function(data,$scope){

            registrationService.registration(data,$scope)
            .then(function (response) {

                return status = response.status;

                console.log("Status1: " + status);
            });     

}


status = getStatus(data,$scope);
console.log("Status2: " + status);

when I move service call outside function all works fine

        registrationService.registration(data,$scope)
        .then(function (response) {

            status = response.status;

            console.log("Status1: " + status);
        }); 

but in this case I'm not able to access status variable outside callback which I need to reuse to check statuses.

JackTheKnife
  • 3,795
  • 8
  • 57
  • 117
  • @baao `return` of what? I have already `return status = response.status` there – JackTheKnife Sep 15 '17 at 18:57
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – baao Sep 15 '17 at 19:00

1 Answers1

1

Couple of things. One, assignments return undefined.

So,

return status = response.status;

Is returning undefined.

Two, a return statment will stop the execution of a function block.

This call below is not console.logging, because that return prevents it from reaching the console.log.

registrationService.registration(data,$scope)
            .then(function (response) {

                return status = response.status;

                console.log("Status1: " + status);
            });   

This call is (that you say is working, which I assume means you're getting that console.log) is working NOT because you pulled it out of the function, but because you removed the return statement.

registrationService.registration(data,$scope)
        .then(function (response) {

            status = response.status;

            console.log("Status1: " + status);
        }); 

Update with more detail:

getStatus returns nothing so it returns undefined. Which means status is getting set to undefined right before your final console log.

status = getStatus(data,$scope);

If registrationService.registration(data,$scope) is async then you're going to have to wait until it resolves before console.logging. Currently you console.log synchronously right after you execute getStatus

Update 2

var status;

 var getStatus = function(data,$scope){

    return registrationService.registration(data,$scope)
            .then(function (response) {

                status = response.status;

                console.log("Status1: " + status);
            });     

}


getStatus(data,$scope)
    .then(function () {
        console.log("Status2: " + status);
    })

Update 3

For the follow up question in the comments below, you should really refactor it like this:

function getStatus (data,$scope){

    return registrationService.registration(data,$scope)
            .then(function (response) {

                if (response.status === "pending") {
                    return getStatus(data, $scope)
                } else if (response.status === "accepted") {
                    // return something else
                } else {
                    // return something else 
                }
                console.log("Status1: " + status);
            });     

}
getStatus(data, $scope)
    .then(function (data) {
        // do whatever you want
    });
jlogan
  • 947
  • 6
  • 9
  • This is not an answer to my issue which is why I'm not able to see value of a `status` variable when functions was used. When `Status2` is `undefinied` not `Status1` – JackTheKnife Sep 15 '17 at 18:55
  • "is async then you're going to have to wait until it resolves" - how do I know when got resolved then? – JackTheKnife Sep 15 '17 at 19:01
  • You need to chain another .then block off of it, and console.log inside the .then – jlogan Sep 15 '17 at 19:01
  • so without `.then` there is no way to do that? Can you give me an example chained `.then` blocks? – JackTheKnife Sep 15 '17 at 19:04
  • 1
    check update 2, that should do it. Note that you have to return the promise out of ```getStatus``` just like @baao was saying. Then you can chain more .then's off of the ```getStatus``` call. – jlogan Sep 15 '17 at 19:06
  • OK, now it works. Further question is how can I loop based on returned status value? There are 3 statements - pending, success, declined. When Is "pending" I need to execute `getStatus` once again to see if ended up with 'success' or 'declined' – JackTheKnife Sep 15 '17 at 19:12
  • Update 3, will call getStatus again if you get a pending. – jlogan Sep 15 '17 at 19:24
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/154560/discussion-between-jlogan-and-jacktheknife). – jlogan Sep 15 '17 at 19:26