SlideShare uma empresa Scribd logo
1 de 26
Baixar para ler offline
Promises

           Luke Smith
           SmugMug
           @ls_n
A promise represents a value
that may not be available yet

      var promise = async();




             promise
A promise represents a value
that may not be available yet

      var promise = async();




             promise




   Synchronous code analogy

       var value = sync();
new Y.Promise(workFn)

function async() {


    function getMessage(resolve, reject) {
        setTimeout(function () {


          resolve(“Promise fulfilled”);

        }, 1000);
    }

    return new Y.Promise(getMessage);        promise


}
then() method to get value when available
or catch errors during value computation

         var promise = async();
         promise.then(onFulfilled, onRejected);




         promise       .then(
                         onFulfilled(value)
                           onRejected(reason)
                       )
then() method to get value when available
     or catch errors during value computation

Synchronous code analogy
var value, error;
try {
    value = sync();          promise   .then(
} catch (reason) {                       onFulfilled(value)
    error = reason;
                                           onRejected(reason)
}
                                       )
if (error) {
    onRejected(reason);
} else {
    onFulfilled(value);
}
Promise Resolution



          Fulfillment                           Rejection


promise        .then(                    promise    .then(
 value           onFulfilled( value )      reason      onFulfilled(value)
                   onRejected(reason)                   onRejected( reason )
               )                                    )
 fulfill                                   reject
then() returns a promise

var promiseA = async();

var promiseB = promiseA.then(onFulfilled, onRejected);




           promiseA       .then(
                            onFulfilled(value)
                              onRejected(reason)
                          )

                               promiseB
then() returns a promise


Synchronous code analogy
var valueA, valueB, error;
try {
    valueA = sync();                promiseA   .then(
} catch (reason) {                               onFulfilled(value)
    error = reason;
                                                   onRejected(reason)
}
                                               )
if (error) {
    onRejected(reason);                             promiseB
} else {
    valueB = onFulfilled(valueA);
}
callback return values fulfill their promises




                      return                                   return
.then(                                  .then(
  onFulfilled(value)            value      onRejected(reason)            value
)                                       )

    promise                                 promise
     value                     fulfill        value                      fulfill
callback errors reject their promises




                      throw                                   throw
.then(                                 .then(
  onFulfilled(value)           error      onRejected(reason)           error
)                                      )

    promise                                promise
    reason                    reject       reason                     reject
Promise Chaining
   var promiseC = async()

                     .then(onFulfilledA, onRejectedA)

                     .then(onFulfilledB, onRejectedB);



promiseA      .then(
                onFulfilledA(value)
                  onRejectedA(reason)
              )

                  promiseB           .then(
                                       onFulfilledB(value)
                                         onRejectedB(reason)
                                     )

                                         promiseC           and so on...
callback resolution through the chain



promise1       .then(                   return
value A          onFulfilled(value) A
                            value                 value B
                   onRejected(reason)
               )
 fulfill                                            fulfill
                   promise2
                    value B
                                            .then(                   return
                                              onFulfilled(value) B
                                                         value                value C
                                                onRejected(reason)
                                            )
                                                                               fulfill
                                                 promise3
                                                 value C
Synchronous code analogy
var valueA, valueB, valueC,
    error = false;

try {
    valueA = sync();
} catch (reasonA) {
    error = true;
    valueA = reasonA;
}

if (error) {
    try {
         valueB = onRejectedA(valueA);
         error = false;
    } catch (reasonB) {
         valueB = reasonB;
    }
} else {
    try {
         valueB = onFulfilledA(valueA);
    } catch (reasonB) {
         error = true;
         valueB = reasonB;
    }
}
Synchronous code analogy
var valueA, valueB, valueC,
    error = false;

try {
    valueA = sync();
} catch (reasonA) {
    error = true;
                                              initial promise generation
    valueA = reasonA;
}

if (error) {
    try {
         valueB = onRejectedA(valueA);
         error = false;
    } catch (reasonB) {
         valueB = reasonB;
    }
} else {
    try {
         valueB = onFulfilledA(valueA);
    } catch (reasonB) {
         error = true;
         valueB = reasonB;
    }
}
} catch (reasonA) {
    error = true;
    valueA = reasonA;
}
                                          Synchronous code analogy
if (error) {
    try {
         valueB = onRejectedA(valueA);
         error = false;
    } catch (reasonB) {
         valueB = reasonB;
    }
} else {                                      for each intermediate promise...
    try {
         valueB = onFulfilledA(valueA);
    } catch (reasonB) {
         error = true;
         valueB = reasonB;
    }
}

if (error) {
    valueC = onRejectedB(valueB);
} else {                                      final promise callbacks
    valueC = onFulfilledB(valueB);
}
value/reason pass thru if callback missing



promise1    .then(
                                         return value B
value A       onFulfilled(value) A
                         value
                onRejected(reason)
            )
 fulfill
                promise2             .then(
                                                          pass thru
                 value B               null
                                         onRejected(reason)
                                     )

                                          promise3            .then(
                                           value B              onFulfilled(value) B
                                                                            value
                                                                  onRejected(reason)
                                                              )
callbacks can return promises

    .then(                 return   returned
      onFulfilled(value)
                                    promise
    )

        promise




    .then(                 return   returned
      onRejected(reason)
                                    promise
    )

        promise
callbacks can return promises

    .then(                 return   returned
      onFulfilled(value)                        Not a value (yet)
                                    promise
    )

        promise




    .then(                 return   returned
      onRejected(reason)                       Not a value (yet)
                                    promise
    )

        promise
returned promises postpone continuation

.then(                    return
  onFulfilled(value)                  returned
                                     promise
)

    promise




               .then(
                 onFulfilled(value)
                   onRejected(reason)
               )
returned promises postpone continuation

.then(                                          inserted
  onFulfilled(value)                  returned              .then(
                                     promise                 onFulfilled(value)
)
                                                               onRejected(reason)
    promise                                                )


                                                           “wait for me”



               .then(
                 onFulfilled(value)
                   onRejected(reason)
               )
returned promises postpone continuation

.then(                                          inserted
  onFulfilled(value)                  returned              .then(
                                     promise                 onFulfilled(value)
)
                                      value                    onRejected(reason)
    promise                                                )

                                      fulfill
                                                           “wait for me”



               .then(
                 onFulfilled(value)
                   onRejected(reason)
               )
returned promises postpone continuation

.then(
  onFulfilled(value)                  returned   .then(
                                     promise      onFulfilled( value )
)
                                      value         onRejected(reason)
    promise                                     )

                                      fulfill




               .then(
                 onFulfilled(value)
                   onRejected(reason)
               )
returned promises postpone continuation

.then(
  onFulfilled(value)                  returned                     .then(
                                     promise                        onFulfilled( value )
)
                                      value                           onRejected(reason)
    promise                                                       )
     value                            fulfill
                                                                                                    .
                                                                                            omise..
                                                                                  i   nal pr
                                                                          e s orig
                                                                  e resolv
                                                        en t valu
               .then(                           f ulfillm
                 onFulfilled(value)
                   onRejected(reason)
               )
returned promises postpone continuation

            .then(
              onFulfilled(value)               returned                     .then(
                                              promise                        onFulfilled( value )
            )
                                                value                          onRejected(reason)
                promise                                                    )
                 value                          fulfill
                                                                                                             .
                                                                                                     omise..
                                                                                           i   nal pr
                                                                                   e s orig
                                                                           e resolv
...then chain continues                                          en t valu
                           .then(                        f ulfillm
                             onFulfilled( value )
                               onRejected(reason)
                           )
Promises/A+ spec
http://promises-aplus.github.com/promises-spec/


   https://github.com/promises-aplus/promises-spec

Mais conteúdo relacionado

Mais de Luke Smith

Mais de Luke Smith (8)

Attribute
AttributeAttribute
Attribute
 
Hack with YUI
Hack with YUIHack with YUI
Hack with YUI
 
Debugging tips in YUI 3
Debugging tips in YUI 3Debugging tips in YUI 3
Debugging tips in YUI 3
 
YUI 3: Events Evolved
YUI 3: Events EvolvedYUI 3: Events Evolved
YUI 3: Events Evolved
 
YUI 3 quick overview
YUI 3 quick overviewYUI 3 quick overview
YUI 3 quick overview
 
YUI 3: Below the Surface
YUI 3: Below the SurfaceYUI 3: Below the Surface
YUI 3: Below the Surface
 
Yui3
Yui3Yui3
Yui3
 
Front end engineering, YUI Gallery, and your future
Front end engineering, YUI Gallery, and your futureFront end engineering, YUI Gallery, and your future
Front end engineering, YUI Gallery, and your future
 

Último

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Último (20)

Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Promises. The basics, from Promises/A+

  • 1. Promises Luke Smith SmugMug @ls_n
  • 2. A promise represents a value that may not be available yet var promise = async(); promise
  • 3. A promise represents a value that may not be available yet var promise = async(); promise Synchronous code analogy var value = sync();
  • 4. new Y.Promise(workFn) function async() { function getMessage(resolve, reject) { setTimeout(function () { resolve(“Promise fulfilled”); }, 1000); } return new Y.Promise(getMessage); promise }
  • 5. then() method to get value when available or catch errors during value computation var promise = async(); promise.then(onFulfilled, onRejected); promise .then( onFulfilled(value) onRejected(reason) )
  • 6. then() method to get value when available or catch errors during value computation Synchronous code analogy var value, error; try { value = sync(); promise .then( } catch (reason) { onFulfilled(value) error = reason; onRejected(reason) } ) if (error) { onRejected(reason); } else { onFulfilled(value); }
  • 7. Promise Resolution Fulfillment Rejection promise .then( promise .then( value onFulfilled( value ) reason onFulfilled(value) onRejected(reason) onRejected( reason ) ) ) fulfill reject
  • 8. then() returns a promise var promiseA = async(); var promiseB = promiseA.then(onFulfilled, onRejected); promiseA .then( onFulfilled(value) onRejected(reason) ) promiseB
  • 9. then() returns a promise Synchronous code analogy var valueA, valueB, error; try { valueA = sync(); promiseA .then( } catch (reason) { onFulfilled(value) error = reason; onRejected(reason) } ) if (error) { onRejected(reason); promiseB } else { valueB = onFulfilled(valueA); }
  • 10. callback return values fulfill their promises return return .then( .then( onFulfilled(value) value onRejected(reason) value ) ) promise promise value fulfill value fulfill
  • 11. callback errors reject their promises throw throw .then( .then( onFulfilled(value) error onRejected(reason) error ) ) promise promise reason reject reason reject
  • 12. Promise Chaining var promiseC = async() .then(onFulfilledA, onRejectedA) .then(onFulfilledB, onRejectedB); promiseA .then( onFulfilledA(value) onRejectedA(reason) ) promiseB .then( onFulfilledB(value) onRejectedB(reason) ) promiseC and so on...
  • 13. callback resolution through the chain promise1 .then( return value A onFulfilled(value) A value value B onRejected(reason) ) fulfill fulfill promise2 value B .then( return onFulfilled(value) B value value C onRejected(reason) ) fulfill promise3 value C
  • 14. Synchronous code analogy var valueA, valueB, valueC, error = false; try { valueA = sync(); } catch (reasonA) { error = true; valueA = reasonA; } if (error) { try { valueB = onRejectedA(valueA); error = false; } catch (reasonB) { valueB = reasonB; } } else { try { valueB = onFulfilledA(valueA); } catch (reasonB) { error = true; valueB = reasonB; } }
  • 15. Synchronous code analogy var valueA, valueB, valueC, error = false; try { valueA = sync(); } catch (reasonA) { error = true; initial promise generation valueA = reasonA; } if (error) { try { valueB = onRejectedA(valueA); error = false; } catch (reasonB) { valueB = reasonB; } } else { try { valueB = onFulfilledA(valueA); } catch (reasonB) { error = true; valueB = reasonB; } }
  • 16. } catch (reasonA) { error = true; valueA = reasonA; } Synchronous code analogy if (error) { try { valueB = onRejectedA(valueA); error = false; } catch (reasonB) { valueB = reasonB; } } else { for each intermediate promise... try { valueB = onFulfilledA(valueA); } catch (reasonB) { error = true; valueB = reasonB; } } if (error) { valueC = onRejectedB(valueB); } else { final promise callbacks valueC = onFulfilledB(valueB); }
  • 17. value/reason pass thru if callback missing promise1 .then( return value B value A onFulfilled(value) A value onRejected(reason) ) fulfill promise2 .then( pass thru value B null onRejected(reason) ) promise3 .then( value B onFulfilled(value) B value onRejected(reason) )
  • 18. callbacks can return promises .then( return returned onFulfilled(value) promise ) promise .then( return returned onRejected(reason) promise ) promise
  • 19. callbacks can return promises .then( return returned onFulfilled(value) Not a value (yet) promise ) promise .then( return returned onRejected(reason) Not a value (yet) promise ) promise
  • 20. returned promises postpone continuation .then( return onFulfilled(value) returned promise ) promise .then( onFulfilled(value) onRejected(reason) )
  • 21. returned promises postpone continuation .then( inserted onFulfilled(value) returned .then( promise onFulfilled(value) ) onRejected(reason) promise ) “wait for me” .then( onFulfilled(value) onRejected(reason) )
  • 22. returned promises postpone continuation .then( inserted onFulfilled(value) returned .then( promise onFulfilled(value) ) value onRejected(reason) promise ) fulfill “wait for me” .then( onFulfilled(value) onRejected(reason) )
  • 23. returned promises postpone continuation .then( onFulfilled(value) returned .then( promise onFulfilled( value ) ) value onRejected(reason) promise ) fulfill .then( onFulfilled(value) onRejected(reason) )
  • 24. returned promises postpone continuation .then( onFulfilled(value) returned .then( promise onFulfilled( value ) ) value onRejected(reason) promise ) value fulfill . omise.. i nal pr e s orig e resolv en t valu .then( f ulfillm onFulfilled(value) onRejected(reason) )
  • 25. returned promises postpone continuation .then( onFulfilled(value) returned .then( promise onFulfilled( value ) ) value onRejected(reason) promise ) value fulfill . omise.. i nal pr e s orig e resolv ...then chain continues en t valu .then( f ulfillm onFulfilled( value ) onRejected(reason) )
  • 26. Promises/A+ spec http://promises-aplus.github.com/promises-spec/ https://github.com/promises-aplus/promises-spec