Shahzad Fateh Ali
Shahzad Fateh Ali

Reputation: 724

Create wrapper function around ajax calls

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");

Upvotes: 0

Views: 158

Answers (1)

Bergi
Bergi

Reputation: 664777

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) {
    …
});

Upvotes: 1

Related Questions