Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Promises, Pipelines, and Asynchrony in ES6 and Beyond

Promises, Pipelines, and Asynchrony in ES6 and Beyond

An introduction to ES6 promises, programming with them, and future developments.

Danielle Brook-Roberge

January 10, 2017
Tweet

More Decks by Danielle Brook-Roberge

Other Decks in Programming

Transcript

  1. Introduction to Promises • A Promise is an object returned

    by an asynchronous function, in place of the eventual value • The Promise can either: – Resolve with the successful output of the process or – Reject with an error • A Promise is settled (resolved or rejected) exactly once. • Upon settlement, the Promise will call any registered handlers.
  2. Promise Handlers • A Promise can have any number of

    handlers registered for when it is settled. • Resolution handlers are registered with the then() method. • Rejection handlers are registered with the catch() method.
  3. Promise Chaining • The then() and catch() methods both return

    Promises, allowing calls to be chained. • Each Promise in the chain resolves to the return value of the previous handler, building a pipeline.
  4. Error Chaining • Return values from both resolution and rejection

    handlers resolve the next step in the chain. • However, if the handler throws an error, the promise will reject with that error.
  5. Promise Chaining Initial Promise then() Handler Handler catch() throw Resolves

    Rejects return throw return then() Handler throw Promise object Rejection Resolution
  6. Promise Flattening • If a Promise handler itself returns a

    Promise, the result of that Promise is passed to the next handler. • This 'flattening' allows chains of asynchronous operations to occur without nesting.
  7. Promise.resolve() • When providing or using an API, it is

    not always known in advance whether a value will be a regular value or a Promise. • The Promise.resolve() function returns a Promise that resolves to the eventual value of its argument, whether that is synchronous or asynchronous.
  8. Promise.all() • The Promise.all() function takes an array of Promises

    and returns a new Promise that resolves with an array of all the values. • If any input Promise rejects, the new promise also rejects.
  9. Advantages of Promises • Promises are the standard way of

    managing asynchronous operations in ES6. • Compared to callback-passing, a Promise-based approach is more predictable: – The handlers are always called asynchronously – They are always called either once or not at all, as required by the result of the asynchronous operation – The function performing the asynchronous operation can just return a value, rather than caring about callbacks. • Promises also enable pipelining, as above, which can be a very readable programming style
  10. Disadvantages of Promises • Promises accentuate the differences between synchronous

    and asynchronous functions • They require an entirely different way of programming that is not always well-matched to the problem at hand • A Promise library must be present, though this is becoming less difficult with time.
  11. Async and Await • In ES2017, there will be a

    friendlier syntax for working with asynchronous functions. • An async function allows its contents to await the result of an asynchronous operation, treating it as a normal value.
  12. Summary • The Promise library in ES6 has become a

    standard feature of the Javascript platform. • Its tools allow asynchronous data to be processed in a pipeline style, with each stage of the pipeline allowed to be asynchronous. • ES2017 will allow a more conventional programming style with the async and await keywords, but will still depend on Promises to make that happen.