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

The promise and the hack

The promise and the hack

This lightning talk presents one pattern to bind components actions in a tree with the use of deferreds objects.

Transcript

  1. The promise and the hack Some names and identifying details

    have been changed to protect the privacy of individuals. @bobylito ParisJS # 43
  2. – @bobylito “Pour bien apprendre quelque chose, il faut en

    abuser”
  3. Promises

  4. Promises call1( params, function( res0 ){ call2( res0, function( res1

    ){ //.... THIS IS HELL!! } ); } );
  5. Promises call1( } ); call1( params ).then( call2 ) .then(

    function peace(){ // At last } );
  6. Deferred objects • The origin of all promises • Remote

    controls of the promises
  7. Deferred objects • Get a promise from it • Set

    the state of the promise state with reject (error etc) or resolve (value)
  8. None
  9. var actions = { colorIt : Q.defer() } actions.colorIt.fCall( theNewColor

    ); var self = this; actions.colorIt.then( function( colorItF ){ self.colorOtherComponent = colorItF; } ); actions.colorIt.resolve( function colorBgComponentA( color ){ //... PROFIT! } )
  10. Benefits • No order required during components initialisation • Documentation

    of all the messages that can be exchange in the app • The root can check if all the actions are resolved
  11. Thanks! • @bobylito (follow me ;) ) • Partners in

    crime : @42loops && @greweb • Check zanimo, qAjax, qAnimationFrame