0

I am new to AngularJS. How can I return the response of "response.data" as a typical function? Because $http generates a promise, when the function finishes it doesn't return the server response.

In my controller I have:

this.message2 = function() {                 
    $http({
        url : 'dataset_of_model',
        method : "POST",
        data : {
            'experiment' : 'rcp85',
            'model' : 'HadGEM2-ES',
            'frequency' : 'day'
        }
    }).then(function(response) {
        console.log('qui');
        console.log(response.data);                               
        return response.data;
    }, function(response) {
        //fail case
        console.log(response);
        console.log(fallito);
        return  response;
    });
};

If I do:

this.message2 = function() {

    var = temp;

    $http({
        url : 'dataset_of_model',
        method : 'POST',
        data : {
            'experiment' : 'rcp85',
            'model' : 'HadGEM2-ES',
            'frequency' : 'day'
        }
    }).then(function(response) {
        console.log('qui');
        console.log(response.data);                                  
        temp = response.data;
    }, function(response) {
        //fail case
        console.log(response);
        console.log(fallito);
        return response;
    });

    return temp;            
}; 

The return temp doesn't have the data because it returns before data, even if I wait for example 10 seconds before return.

How can I return data in a synchronous way?

Thanks!!

2
  • I think there is no way. Javascript is single-threaded and can never afford to stop and wait for a certain function to complete. Commented Jul 21, 2016 at 15:36
  • 1
    Possible duplicate of How do I return the response from an asynchronous call? Commented Jul 21, 2016 at 15:47

2 Answers 2

0

Try this.

this.getResults = function(){
    return $http.get(url).then(function(res){
        //Resquest successs
    }, function(err){
        //Resquest fail
    });
}

And then

MyFooService.getResults().then(...);
Sign up to request clarification or add additional context in comments.

7 Comments

Why not just return $http?
So you can optionally process the response before resolving the promise. Like cleaning the output, aggregate results, etc. But yes, also works just returning the $http;
Right, you can do all of that without using $q. $http already returns a promise.
Hi Kevin B: if I do: return $http({ url : 'dataset_of_model', .... ... the result is null!
Probably because you aren't waiting for it to complete. see the duplicate.
|
-1

I'm not sure why would would want to do that? You're returning response.data anyways in the first option. You should also probably add return before the $http (that way you're returning the promise). However, anything that you want to happen synchronized with the response must be in the .then block. For example, if you wanted to get a response of all goals scored, then add that number to some variable, you'd have to put that functionality in the .then block, then return the response.data at the end of the block.

That way, the functions executed inside the then block are synchronous.

You have no guarantee over the functions executed outside the then block, unless you want to hack it with $timeout (stop for 2 secs, then execute that return temp), which I don't recommend.

Comments

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.