SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Mehr von Luke Smith

Mehr von 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
 

Kürzlich hochgeladen

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
 

Kürzlich hochgeladen (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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
 
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
 
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
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
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
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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)
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

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