SlideShare a Scribd company logo
1 of 26
Download to read 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

More Related Content

More from Luke Smith

Debugging tips in YUI 3
Debugging tips in YUI 3Debugging tips in YUI 3
Debugging tips in YUI 3Luke Smith
 
YUI 3: Events Evolved
YUI 3: Events EvolvedYUI 3: Events Evolved
YUI 3: Events EvolvedLuke Smith
 
YUI 3 quick overview
YUI 3 quick overviewYUI 3 quick overview
YUI 3 quick overviewLuke Smith
 
YUI 3: Below the Surface
YUI 3: Below the SurfaceYUI 3: Below the Surface
YUI 3: Below the SurfaceLuke Smith
 
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 futureLuke Smith
 

More from 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
 

Recently uploaded

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 CVKhem
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
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 Processorsdebabhi2
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
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 Takeoffsammart93
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
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 2024The Digital Insurer
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
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...apidays
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 

Recently uploaded (20)

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
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
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
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 

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