1

I want to get promise of asynchronous call without writing any service. I have written some API to get data from back-end in JavaScript, but following code is not written in any service. I want to access the following promise in another JavaScript file. How can I access the promise in different file?

  var cust_Promise = $q.defer() ;    
  var apt_data={some data};
  $http.post(api,apt_data) 
    .success(function (response,status) {      
        cust_Promise.resolve('Success');
        return cust_Promise.promise ;              
    }) ;
georgeawg
  • 48,608
  • 13
  • 72
  • 95
user1308874
  • 61
  • 10

1 Answers1

1

The $http service returns promises.

Simply save the returned promise.

var apt_data = {some data};

var promise = $http.post(url, apt_data);

This means the AngularJS framework supports passing them to other functions, returning them as the values from other functions, and assigning them to variables or storing them in data structures.

The fulfilled results or rejected error information can be retrieved by using the standard .then and .catch methods.

var derivedPromise = promise.then (function onfullFilled(response) {
    $scope.data = results.data;
    //return for chaining
    return response;
}).catch ( function onRejected(response) {
    console.log(response.statusText);
    //throw to chain rejection
    throw response;
});

Because calling the .then method of a promise returns a new derived promise, it is easily possible to create a chain of promises. It is possible to create chains of any length and since a promise can be resolved with another promise (which will defer its resolution further), it is possible to pause/defer resolution of the promises at any point in the chain. This makes it possible to implement powerful APIs.1

From the Docs:

The $http API is based on the deferred/promise APIs exposed by the $q service. While for simple usage patterns this doesn't matter much, for advanced usage it is important to familiarize yourself with these APIs and the guarantees they provide.

General usage:2

The $http service is a function which takes a single argument — a configuration object — that is used to generate an HTTP request and returns a promise.

The response object has these properties:

  • data – {string|Object} – The response body transformed with the transform functions.
  • status – {number} – HTTP status code of the response.
  • headers – {function([headerName])} – Header getter function.
  • config – {Object} – The configuration object that was used to generate the request.
  • statusText – {string} – HTTP status text of the response.

A response status code between 200 and 299 is considered a success status and will result in the success callback being called. Note that if the response is a redirect, XMLHttpRequest will transparently follow it, meaning that the error callback will not be called for such responses.

Additional Resources

Community
  • 1
  • 1
georgeawg
  • 48,608
  • 13
  • 72
  • 95