Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the HTTP promise object in AngularJS?

Although I am working with the HTTP promise object in AngularJS, I don't have a clear concept of what an HTTP promise object actually is and what the is difference between an HTTP promise object and a traditional object in AngularJS!

Would anybody explain this, please?

like image 954
TanvirArjel Avatar asked Nov 20 '16 14:11

TanvirArjel


People also ask

What is a http Promise?

A promise represents a single result of an asynchronous operation. It is not necessarily available at a specific time, but should become in the future. The PHP-HTTP promise follows the Promises/A+ standard.

What is a Promise in AngularJS?

Promises in AngularJS are provided by the built-in $q service. They provide a way to execute asynchronous functions in series by registering them with a promise object. {info} Promises have made their way into native JavaScript as part of the ES6 specification.

What is Promise method in Angular?

What Is Promise in Angular? Promises in Angular provide an easy way to execute asynchronous functions that use callbacks, while emitting and completing (resolving or rejecting) one value at a time. When using an Angular Promise, you are enabled to emit a single event from the API.

What is http in Angular?

$http is an AngularJS service for reading data from remote servers.


1 Answers

A Promise is a concept for asynchronous operations. Basically it represents an object that can be available at any point from now into the future.

It has three states:

  • Pending
  • Fulfilled (it completed successfully)
  • Rejected (it failed)

You handle the states of your Promise with two methods, then() and catch().

then() provides you with the expected object from your asynchronous call if successful, and catch() will allow you to handle the error.

A scenario where you might use a Promise is when you're making a network call, for example:

getData(): Promise<Array<string>> {
    return this.http.get("http://a-test-api.com/api/getdata").toPromise();
}

You'd then use it like this:

this.getData().then(function (stringArray) {
        self.data = stringArray;
});

You can find some more information on the concept here: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise

like image 97
JCrook1121 Avatar answered Oct 05 '22 23:10

JCrook1121