A promise can either be fulfilled or it can be rejected.
var promise = fetchTheAnswer(); promise.then(fulfill, reject); function fulfill(answer) { console.log("The answer is " + answer); } function reject(reason) { console.log("Broken promise! Reason: " + reason); }
You can even chain promises together with .then()
and run through a whole slew of asynchronous actions.
var promise = Ember.$.getJSON('/promise-me.json'); promise.then(doThis) .then(doThat) .then(doOtherStuff) .then(handleFulfill, handleReject);
A promise is a method with asynchronous behavior. It returns a "thennable" which is an object that reflects fulfillment (success) or unfulfillment (failure).
You attach handlers for these states using the then
method. Each then
returns yet another promise
.
More about asynchronous routing.