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

A promise is a Promise (dotJSEU edition)

A promise is a Promise (dotJSEU edition)

Super compressed version of "A promise is a Promise" for the 2012 #dotjs conference in Paris.

David Iglesias

November 28, 2012
Tweet

More Decks by David Iglesias

Other Decks in Programming

Transcript

  1. A promise is a Promise Sync events without the Nasty

    Hacks™ David Iglesias Senior Engineer (Front-end Framework) [ twitter | speakerdeck ].com/ditman
  2. Event-driven code class PriceComparator { - providers = [ ProvA,

    ProvB, ProvC, ... ]; - handleProviderDone(provider, price) { // @todo store the price... } + computePrice (param1, param2, param3) { foreach (providers as provider) { provider.on('done', handleProviderDone); provider.computePrice(param1, param2, param3); } // @todo When all providers are done, show them } }
  3. • Event listeners must be subscribed before the events are

    fired • Knowing when all "providers" are done is hard not straightforward Problems / hard things Naïve solutions turn into Nasty Hacks™
  4. • Events with memory ◦ They remember being fired •

    Composed of 2 parts ◦ Promise (handles event subscriptions) ◦ Resolver (decides if the promise was success / fail) • Synchronization mechanisms ◦ all, any, some, chain ... Deferred to the rescue! (when.js) ...and they have been around since the '70s!
  5. • Late subscription ◦ Subscribe to an event seconds after

    it's been fired • Synchronization ◦ Synchronize 100 slowness providers Examples
  6. We're hiring! (of course!) Talk to us! We'll be around!

    Shameless plug: Thanks! speakerdeck.com/ditman @ditman [email protected]