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!!

κροκς
  • 592
  • 5
  • 18
  • I think there is no way. Javascript is single-threaded and can never afford to stop and wait for a certain function to complete. – hylimR Jul 21 '16 at 15:36
  • 1
    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) – Kevin B Jul 21 '16 at 15:47

2 Answers2

0

Try this.

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

And then

MyFooService.getResults().then(...);
alejandromav
  • 933
  • 1
  • 11
  • 24
-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.

McFiddlyWiddly
  • 547
  • 10
  • 29