0

I want to create a wrapper around my API calls in AngularJS like

function MakeCall (url){
$http.get(url, config).success(successfunction);

return response;
}

Can you help me how to wait for the call to complete and get the final response from the call. In above code the "return response" is what I want to get after the completion of call. This function will be used like

response = MakeCall("to some url");

1 Answers1

1
response = MakeCall("to some url");

That's not how promises work. You cannot synchronously return the result of an ajax call.

You just seem to want to return the promise that $http.get() already yields from your function:

function MakeCall (url){
    return $http.get(url, config).success(successfunction);
}

Then use it like this:

MakeCall("to some url").then(function(response) {
    …
});
Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375