-
Notifications
You must be signed in to change notification settings - Fork 9
Using promises
abaaso implements the Promises/A+ specification.
Promises, or deferreds or futures, are a way to create a tangible connection between now and an eventual outcome, with the lowest possible latency. Promises are never guaranteed to be resolved.
When you chain a Promise's then, you are making a series of Promises. Chaining is a way to branch from a single outcome.
Chaining Promises is like a game of "telephone", it's communication. Promises "talk" to each other through return values, and the context is how that value is transmitted. If you have a chain of Promises, and the resolution starts as a rejection (error), it'll continue to be a rejection until a Promise in the chain doesn't throw an Error, causing the next Promise to treat the value as a fulfillment (success). If the last Promise resolves as a rejection (error), it'll remain as such even if it doesn't throw an Error, because it has nothing to communicate, it's the end of the "telephone" game.
var d1 = $.promise(),
d2;
d2 = d1.then(…).then(…).then(…).then(…);
d2.resolve("something"); // d1 will resolve last, with the eventual value passed from d2 through the chainIf a promise returns a new promise, a fork is created and the resolution halts in a pending state. This state can only be resolved from the new fork.