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.

2

1 Answer 1

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
    });
Sign up to request clarification or add additional context in comments.

8 Comments

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
"is async then you're going to have to wait until it resolves" - how do I know when got resolved then?
You need to chain another .then block off of it, and console.log inside the .then
so without .then there is no way to do that? Can you give me an example chained .then blocks?
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.
|

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.