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

A promise is a promise

A promise is a promise

LINKS ARE CLICKABLE! DOWNLOAD THE SLIDES TO SEE DOCUMENTATION, DEMOS, ETC...

JavaScript event synchronization and late subscription using Promises, with when.js

[English version]

David Iglesias

November 17, 2012
Tweet

More Decks by David Iglesias

Other Decks in Programming

Transcript

  1. How does it work? • Events ◦ "Things that happen"

    • Handlers / Callbacks ◦ They react to events
  2. Tiny example class Balumba extends CarInsurance { - handleDone (price)

    { fire('done', Balumba, price); } + computePrice (carModel, age, gender) { BalumbaServer.on('done', handleDone); BalumbaServer.on('error', handleError); BalumbaServer.request(carModel, age, gender); } }
  3. Let's make our life harder class Rastreator { - providers

    = [ Balumba, Genesis, Verti, ... ]; - handleProviderDone(provider, price) {} + computePrice (carModel, age, gender) { foreach (providers as provider) { provider.on('done', handleProviderDone); provider.computePrice(carModel, age, gender); } // When all providers are done, show them } }
  4. No problemo! • Promises (AKA Future / Delay / Deferred)

    ◦ Proposed in 1976-77 ◦ Implemented in: ABCL/f, AmbientTalk, Io, Lucid, Oxygene, Oz, R, Scheme...
  5. Deferred (when.js) • Events "with memory" ◦ They "remember" being

    fired • Divided in 2 parts ◦ Promise (handles event subscriptions) ◦ Resolver (decides if the Promise gets resolved/rejected) • Synchronization ◦ all, any, some • Pipelining ◦ chain