What Is A Promise Object?
Solution 1:
A promise is a new Object type of EcmaScript 6 (ES6), for which there are numerous polyfills and libaries (i.e. implementations for ES5 JavaScript engines), and allows (among other benefits) to get out of the infamous callback hell, and to write and read asynchronous code easily.
A promise can have one (and only one) of these three status:
- pending
- fulfilled
- rejected
If the promise is rejected or fulfilled, it also has a settled status.
Basically, it is an object that has a then
property (amongst others), which is a function
that takes at least one function as a parameter, and can take two: the first one will be invoked if the promise returns a fulfilled status, and the second one will be invoked if the promise returns a rejected status
The then
function returns itself another promise, so promises are chainable.
Promise objects are rather more complicated than what I just wrote here, but it was just to give you a start.
BTW, you may have used a promise-like object (note the -like suffix) if you use jQuery : $.ajax()
returns a promise-like object (those are called thenables) that has a done
(and a then
) property which is a function
that accepts a function as the parameter that seems like a fulfilled
function (which normally takes only one argument). Promise objects also may have a done
function property (not standardised, AFAIK, but almost all the polyfills and the libraries implement it), which acts like a then function, only it does not return a promise (hence the name: if you are done with the promise, then use done()
, but if you need to do somehting with the result of the promise, use then()
).
e.g.: you may have seen or written something like this:
$.ajax({url: '/path/to/html/piece'})
.done(function(data) {
$('whateverSelector').html(data);
});
But what jQuery calls promises, even if they are thenables, does not fulfill the promise spec.
Solution 2:
Promise object is what it's name says it is - a promise. In frontend frameworks like Ember and Angular promise is an object returned by an asynchronous call. This call instead of blocking the whole system returns you a promise, which will eventually contain the data the asynchronous call returns.
Promises have an event called resolve, which is triggered when the response come. That's why promise objects have method usually called then
. You can use it like this (after Ember.js docs):
var promise = fetchTheAnswer();
promise.then(fulfill, reject);
functionfulfill(answer) {
console.log("The answer is " + answer);
}
functionreject(reason) {
console.log("Couldn't get the answer! Reason: " + reason);
}
When your request is fulfilled the promise will call the method fulfill
with answer
(response) as an argument, and when the request is rejected (ie. when the resource doesn't exist) it will call method reject
with reason
(error) as an argument.
Such objects are similar to objects in other OO languages like java - they contain both data and methods you can call on them.
Post a Comment for "What Is A Promise Object?"