1) Promises are objects that represent the eventual completion (or failure) of an asynchronous operation, rather than using callbacks. They provide a cleaner way to handle asynchronous code than callbacks alone.
2) A promise has three possible states: pending, fulfilled, and rejected. Promises allow multiple handlers to be attached to an asynchronous operation via the .then() method. Handlers will be called even if attached after the promise is settled.
3) Common uses of promises include handling data retrieval from external sources, loading assets, and handling animations. Promises are well-suited for any potentially asynchronous operation.
11. The A+ Spec
• 3 states: pending, fulfilled, rejected (settled*)
• .then(), which takes handler functions as params
• Can call multiple times; attach multiple handlers
• Handler called even if then() called after settled
• Once fulfilled or rejected, cannot change state
13. .then(onFulfill, onReject)
• onFulfill called when promise is fulfilled
• onReject called when promise is rejected
• Handler execution deferred to nextTick
• Handlers have one argument
• Return val becomes promise returned by then()***
31. Common uses of promises
• Data retrieval
• Anything involving an external call
• Loading CSS
• Animation
• Template rendering
32. Promise vs Event vs Stream
• Promises are for operations executed once
• Events and streams are for repeated events
• Promises and streams are both object-based
33. High-order functions
• Functions that input and/or output functions
• Examples
• _.partial(func, arg)
• _.bind(func, context)
• Very useful with promises
36. Error handling
• Error in onFulfill will reject returned promise
• Throwing rejects next promise in chain
• Non A+ features
• Long stack traces
• Global error listener