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

Promises in JavaScript

Promises in JavaScript

Revath S Kumar

October 27, 2013
Tweet

More Decks by Revath S Kumar

Other Decks in Programming

Transcript

  1. ABOUT ME Rubyist / JavaScripter Yeoman team member Twitter/Github -

    @revathskumar http://blog.revathskumar.com
  2. WHAT ARE PROMISES? A pattern to work with asyncronous operations

    much neatly. A function return a object called promise, which is the eventual result of the operation. operated via then method. States : Pending / Fulfilled / Rejected p r o m i s e . t h e n ( o n F u l f i l l e d , o n R e j e c t e d )
  3. U s e r . g e t ( 1

    , { s u c c e s s : f u n c t i o n ( ) { c o n s o l e . l o g ( ' S u c c e s s ' ) ; } , e r r o r : f u n c t i o n ( ) { c o n s o l e . l o g ( ' E r r o r ' ) ; } } )
  4. IN PROMISE U s e r . g e t

    ( 1 ) . t h e n ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' S u c c e s s ' ) ; } , f u n c t i o n ( ) { c o n s o l e . l o g ( ' E r r o r ' ) ; } ) ;
  5. U s e r . g e t ( '

    1 ' , { s u c c e s s : f u n c t i o n ( ) { A c c o u n t s . g e t ( ' 1 ' , { s u c c e s s : f u n c t i o n ( ) { A c c o u n t s . u p d a t e ( { t w i t t e r : @ a d d y o s m a n i } , { s u c c e s s : f u c n t i o n ( ) { c o n s o l e . l o g ( ' S u c c e s s ' ) } , e r r o r : f u n c t i o n ( ) { c o n s o l e . l o g ( ' E r r o r ' ) ; } } ) } , e r r o r : f u c n t i o n ( ) { c o n s o l e . l o g ( ' E r r o r ' ) ; } } ) } , e r r o r : f u n c t i o n ( ) { c o n s o l e . l o g ( ' e r r o r ' ) ; } } ) ;
  6. U s e r . g e t ( 1

    ) . t h e n ( A c c o u n t s . g e t ) . t h e n ( f u n c t i o n ( ) { r e t u r n A c c o u n t s . u p d a t e ( { t w i t t e r : @ a d d y o s m a n i } ) } ) . t h e n ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' S u c c e s s ' ) ; } ) . t h e n ( u n d e f i n e d , f u n c t i o n ( ) { c o n s o l e . l o g ( ' E r r o r ' ) ; } ) ; v a r U s e r = ( f u n c t i o n ( ) { f u n c t i o n g e t ( u s e r _ i d ) { v a r p r o m i s e = n e w A p l u s ( ) ; $ . g e t ( ' / u s e r s / ' + u s e r _ i d , { s u c c e s s : f u n c t i o n ( d a t a ) { p r o m i s e . f u l f i l l ( d a t a . a c c o u n t _ i d ) ; } , e r r o r : f u n c t i o n ( ) { p r o m i s e . r e j e c t ( ' E r r o r ' ) ; } } ) ; r e t u r n p r o m i s e ; } r e t u r n { g e t : g e t } } )
  7. POOLING A p l u s . p o o

    l ( a s y n c F u n c ( ) , a n o t h e r A s y n c F u n c ( ) ) . t h e n ( a s y n c S u c c e s s , a s y n c E r r o r ) ;
  8. USING JQUERY $ . w h e n ( $

    . a j a x ( " / p a g e 1 . p h p " ) , $ . a j a x ( " / p a g e 2 . p h p " ) , ) . t h e n ( o n S u c c e s s , o n E r r o r ) ;
  9. JQUERY : MULTIPLE CALLBACKS $ . w h e n

    ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' H e l l o P r o m i s e ' ) ; } ) . t h e n ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' S u c c e s s 1 ' ) ; } , f u n c t i o n ( ) { c o n s o l e . l o g ( ' E r r o r 1 ' ) ; } ) . t h e n ( f u n c t i o n ( ) { c o n s o l e . l o g ( ' S u c c e s s 2 ' ) ; } , f u n c t i o n ( ) { c o n s o l e . l o g ( ' E r r o r 2 ' ) ; } ) ;