The promise and the
hack
Some names and identifying details have been changed
to protect the privacy of individuals.
@bobylito ParisJS # 43
Slide 2
Slide 2 text
– @bobylito
“Pour bien apprendre quelque chose, il faut en
abuser”
Slide 3
Slide 3 text
Promises
Slide 4
Slide 4 text
Promises
call1( params, function( res0 ){
call2( res0, function( res1 ){
//.... THIS IS HELL!!
} );
} );
Slide 5
Slide 5 text
Promises
call1(
} );
call1( params ).then( call2 )
.then( function peace(){
// At last
} );
Slide 6
Slide 6 text
Deferred objects
• The origin of all promises
• Remote controls of the promises
Slide 7
Slide 7 text
Deferred objects
• Get a promise from it
• Set the state of the promise state with reject
(error etc) or resolve (value)
Slide 8
Slide 8 text
No content
Slide 9
Slide 9 text
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!
}
)
Slide 10
Slide 10 text
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
Slide 11
Slide 11 text
Thanks!
• @bobylito (follow me ;) )
• Partners in crime : @42loops && @greweb
• Check zanimo, qAjax, qAnimationFrame