SlideShare a Scribd company logo
///
Promises/A+
upstream from spec
● state
○ pending
○ fulfilled
○ rejected
● value
○ when
fulfilled
● reason
○ When
rejected
ECMAScript 2020 (draft)
‘the spec’
● [[PromiseState]]
○ pending
○ fulfilled
○ rejected
● [[PromiseResult]]
○ when
fulfilled
○ when
rejected
FireFox
downstream from spec
● <state>
○ pending
○ fulfilled
○ rejected
● <value>
○ when
fulfilled
● <reason>
○ When
rejected
Item
● [object Promise]
properties
Chrome
downstream from spec
● [[PromiseStatus]]
○ pending
○ resolved
○ rejected
● [[PromiseValue]]
○ when
resolved
○ when
rejected
THIS IS THE ONLY SLIDE WHICH IS
BOTH SUCCINCT AND INTERESTING
Deciphering: ‘Promise state’ and ‘Promise status’ across Implementations
● SPEC: TC39 (2019-11 https://tc39.es/ecma262/#sec-properties-of-promise-instances )
○ Refers to [ ‘pending’, ‘fulfilled’, ‘rejected’ ] as possible values of a (new Promise).[[PromiseState]]
○ Makes no reference to ‘status’ of a Promise
● Both:
○ (downstream) MDN (2019-11 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise )
○ (upstream) Promises/A+ (2019-11 https://promisesaplus.com/)
■ Refer to [ ‘pending’, ‘fulfilled’, ‘rejected’ ] as ‘states’ of a Promise
■ Makes no reference to ‘status’ of a Promise
■ (At this time Firefox follows this implementation, implementing (new Promise).<state> )
● (downstream) Chrome (2019-11 Version 78.0.3904.108 (Official Build) (64-bit) )
○ Does not implement (new Promise).[[PromiseState]]
○ Instead, it implements (new Promise).[[PromiseStatus]], synonymously.
○ Refers to [ ‘pending’, ‘resolved’, ‘rejected’ ] as possible values of a (new Promise).[[PromiseStatus]]
APPENDICE,
SKIPPABLE
Deciphering: ‘Promise result’, ‘Promise value’, and ‘Promise reason’ across Implementations
● SPEC: TC39 (2019-11 https://tc39.es/ecma262/#sec-properties-of-promise-instances )
○ If [[PromiseState]] is ‘fulfilled’ or ‘rejected’, then the Promise’s value (informal) may be found in (new
Promise).[[PromiseResult]]
● Both:
○ (downstream) MDN (2019-11 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise )
○ (upstream) Promises/A+ (2019-11 https://promisesaplus.com/)
■ Refers to Promises ‘fulfilled’ with a ‘value’
■ Refers to Promises ‘rejected’ with a ‘reason’
■ Refers to both of the above, informally, as ‘results’ of a Promise
■ (At this time Firefox follows this implementation, implementing (new Promise).<value> and (new Promise).<reason>.)
● (downstream) Chrome (2019-11 Version 78.0.3904.108 (Official Build) (64-bit) )
○ Does not implement [[PromiseResult]]
○ Instead, it implements (new Promise).[[PromiseValue]], synonymously.
APPENDICE,
SKIPPABLE
///
new Promise ( executor_fn )
When the Promise constructor is called:
1. It creates two built-in
functions (BIFs) via a
subroutine.
2. Then, it calls ‘executor_fn’,
passing the BIFs as arguments.
The BIFs are often documented as
‘resolve_fn’ and ‘reject_fn’. But to
reduce ambiguity, I recommend that you
call them ‘fulfill_fn’ and ‘reject_fn’
instead. This makes their names more
consistent with the names of underlying
clauses in the ECMA specification. It
also reduces the ambiguity of
‘resolution’, which should refer to both
‘fulfillment’ and ‘rejection’.
executor_fn
( fulfill_fn@resolve_fn
, reject_fn )
CreateResolvingFunctions ( promise )calls
calls
fulfill_fn@resolve_fn
, reject_fn
returns [object Promise]
RejectPromise ( promise, reason )
referencing
steps from
FulfillPromise ( promise, value )
referencing
steps from
TriggerPromiseReactions
( promiseRejectionReactions, argument )
referencing
steps from
returns
Deciphering: ‘Promise resolution’ and ‘Promise rejection’ across Implementations
● SPEC: TC39 (2019-11 https://tc39.es/ecma262/# )
○ 25.6.3.1 Promise ( executor ), the (new Promise) construction makes a call to…
■ 25.6.1.3 CreateResolvingFunctions ( promise )
● … containing the first use of the base word ‘resolve’…
● … returning ‘resolve’ and ‘reject’ as two new built-in functions
● … and is defined in terms of:
○ … the steps: 25.6.1.3.1 Promise Reject Functions, which runs 25.6.1.7 RejectPromise ( promise, reason )
which runs 25.6.1.8 TriggerPromiseReactions ( reactions, argument ) which ingests the list…
■ promise.[[PromiseRejectReactions]] (which was built by 25.6.5.4.1 PerformPromiseThen)
■ (These two clauses are consistent in their nomenclature - ‘reject, reject’.)
○ … the steps: 25.6.1.3.2 Promise Resolve Functions, which runs 25.6.1.4 FulfillPromise ( promise, value )
which runs, 25.6.1.8 TriggerPromiseReactions ( reactions, argument ) which ingests the list…
■ promise.[[PromiseFulfillReactions]] (which was built by 25.6.5.4.1 PerformPromiseThen)
■ (These two clauses are inconsistent1
in their nomenclature - ‘resolve, fulfill’.)
● So here we see another inconsistency2
, where
○ 25.6.1.3 refers to both of the following as ‘resolving functions’:
■ 25.6.1.3.1, which is itself not called ‘resolving’, and
■ 25.6.1.3.2, which is itself called ‘resolving’
● (Comment: 25.6.1.3.2 should be called Promise Fulfill Functions, which would avoid both inconsistencies1&2
.)
■ ( … continued below … )
APPENDICE,
SKIPPABLE
///
p = new Promise
(executor_fn)
/* returns a
[object Promise]
*/
/* Since the Promise constructor calls
executor_fn, fulfill_fn and reject_fn are defined,
and passed to executor_fn, by the constructor (not
user); yet they are called by the user. The user
defines executor_fn, and it is called before (new
Promise) returns [object Promise]. Example: */
executor_fn = function ( fulfill_fn, reject_fn ) {
/* State of the returned [object Promise]:
1. So long as neither fulfill_fn nor reject_fn
are called: pending
2. If resolve_fn is called here: fulfilled
3. If rejected_fn is called here: rejected */
}
/* There are two key
methods of p which we
can call:
1. p.then(
onFulfilled_fn,
onRejected_fn )
2. p.catch(
onRejected_fn )
*/
NEXT PAGE
NEXT PAGE
/* The user defines onFulfilled_fn and
onRejected_fn, but these are called by
then or catch. Both parameters are
optional, and each takes a single
parameter.
onFulfilled_fn defaults to the
identity function, returning the
received argument; otherwise it can be
defined by the user as
(fulfilmentValue => {
// do something
})
onRejected_fn defaults to a function,
which throws the received reason;
otherwise it can be defined by the
user as (rejectionReason => {
// do something;
// rejectionReason is often but
// not always an error
})
*/
q = p.then(
onFulfilled_fn,
onRejected_fn )
/* returns a
[object
Promise] */
r = p.catch(
onRejected_fn )
/* Internally calls
p.then(undefined,
onRejected_fn);
returns a [object
Promise] */
PREVIOUS PAGE
/* If any handler ( onFulfilled_fn or onRejected_fn ) …
● … does not return any value, then q’s returned state will be resolved, and q’s returned value will be undefined
● … returns a value, v, then q’s returned state will be resolved, and q’s returned value will be v
● … throws an error, e, then q’s returned state will be rejected, and q’s returned value will be e
● … returns a Promise, s, where
○ s is fulfilled, and s’s value is v, then q’s returned state will be fulfilled, and q’s returned value will be v
○ s is rejected, and s’s value is v, then q’s returned state will be rejected, and q’s returned value will be v
○ s is pending, then q return s
*/
PREVIOUS PAGE
If [object Promise]’s state is PENDING, then
● pushes fulfillReactionPromiseReactionRecord
onto the
list [object Promise]. PromiseFulfillReactions
● pushes rejectReactionPromiseReactionRecord
onto the
list [object Promise]. PromiseRejectReactions
Promise.prototype.then
( onFufill_fn, onReject_fn )
referencing
steps from
resultCapabilityPromiseCapabilityRecord
or ‘undefined’returns
If [object Promise]’s state is FULFILLED, then
● enqueues a PromiseReactionJob based on
fulfillReactionPromiseReactionRecord
If [object Promise]’s state is REJECTED, then
● enqueues a PromiseReactionJob based on
rejectReactionPromiseReactionRecord
PerformPromiseThen (promise, onFulfilled, onRejected,
resultCapabilityPromiseCapabilityRecord
)
● Creates fulfillReactionPromiseReactionRecord
and
rejectReactionPromiseReactionRecord
calls
resultCapabilityPromiseCapabilityRecord
returns
NewPromiseCapability
( SpeciesConstructor(promise, %Promise%) )
● … which makes a (new Promise), but lets you modify the
fulfill_fn and reject_fn which are passed into the new
object’s executor_fn
Promise.resolve ( value )
● This has a special role. If ‘value’ is a Promise
or thenable, it will not be wrapped in a new
Promise.
referencing
steps from
● If ‘value’s is an Object, and a Promise,
then return ‘value’, without
modification;
● elseif ‘value’s is an Object, and a
thenable, but not a Promise, then attempt
to coerce/convert the thenable to a
Promise, and then return that Promise
without modification;
● otherwise return :
new Promise ( (ff,rj) => { ff (value) } )
returns
PromiseResolve ( let’s not get into this)
Promise.reject ( reason )
● Returns :
new Promise ( (ff,rj) => { rj (reason) } )
● This always wraps ‘reason’ in a new rejected
Promise.
Deciphering: ‘perform promise then’, ‘promise reaction records’
● SPEC: TC39 (2019-11 https://tc39.es/ecma262/# )
○ 25.6.5.4 Promise.prototype.then ( onFulfilled, onRejected )
■ … sets resultCapability = 25.6.1.5 NewPromiseCapability ( C )
■ … returns 25.6.5.4.1 PerformPromiseThen ( promise, onFulfilled, onRejected [ , resultCapability ] ) …
● … is called by
○ 6.2.3.1 Await
○ 25.1.4.4 AsyncFromSyncIteratorContinuation ( result, promiseCapability )
○ 25.5.3.5 AsyncGeneratorResumeNext ( generator )
○ 25.6.5.4 Promise.prototype.then ( onFulfilled, onRejected )
● … and (each time it is called, on the same promise) creates 25.6.1.2 PromiseReaction Records
○ … and pushes them onto
■ promise.[[PromiseFulfillReactions]], and
■ promise.[[PromiseRejectReactions]]
○ … from where they are later consumed in the next step…
● … and calls 8.4.1 EnqueueJob ( queueName, job, arguments )…
○ … which puts a 25.6.2.1 PromiseReactionJob ( reaction, argument ) into the ‘PromiseJobs’ queue
APPENDICE,
SKIPPABLE
Deciphering: ‘promise capability records’ and ‘result capability’
● SPEC: TC39 (2019-11 https://tc39.es/ecma262/# )
○ 25.6.1.5 NewPromiseCapability ( C ) …
■ … creates 25.6.1.1 PromiseCapability Records …
● … sometimes playing the role of i.e. used as ‘result capabilities’
■ … which are ingested by (for example)…
● 12.3.7 Import Calls , Async { Function Definitions - 14.7.11 Runtime Semantics: EvaluateBody , Arrow Function Definitions - 14.8.15 Runtime
Semantics: EvaluateBody }, 25.1.4.2 The %AsyncFromSyncIteratorPrototype% Object - next(), return(), throw(), 25.5.3.6 AsyncGeneratorEnqueue
( generator, completion ), 25.6.4.1 Promise.all ( iterable ) , 25.6.4.2 Promise.allSettled ( iterable ) , 25.6.4.4 Promise.race ( iterable )
, 25.6.4.5 Promise.reject ( r ) , 25.6.4.6.1 PromiseResolve ( C, x ) , 25.6.5.4 Promise.prototype.then ( onFulfilled, onRejected )
○ Distinct from the built-in functions ‘resolve’ and ‘reject’ created by 25.6.1.3 CreateResolvingFunctions ( promise ),
■ there also exist two UNRELATED functions
● 25.6.4.6 Promise.resolve ( x ),
○ which returns 25.6.4.6.1 PromiseResolve ( C, x ), which has a ‘flattening’ role
● 25.6.4.5 Promise.reject ( r ) ,
■ both of which call NewPromiseCapability
APPENDICE,
SKIPPABLE
///
Readings
● Promises/A+
○ An open standard for sound, interoperable JavaScript promises.
○ This is the standard implemented below.
● Draft ECMA-262 / (November) ECMAScript® 2020 Language Specification
○ This document is the abstract specification, independent of any specific language implementation.
○ 25.6 Promise Objects
■ This is a high-level introduction to the usage of this object.
■ 25.6.3.1 Promise ( executor )
● This is a good place to start reading, because it is generally the first call that people make to this
object/class. The downstream MDN documentation introduces the reader to Promises from here.
■ 25.6.6 Properties of Promise Instances
● This is also a good place to start reading, as it addresses fundamental definitions.
● Community
○ https://stackoverflow.com/questions/29268569/what-is-the-correct-terminology-for-javascript-promises
APPENDICE,
SKIPPABLE
///
CONTINUE HERE

More Related Content

PPTX
ES2015 promise
PDF
Async js - Nemetschek Presentaion @ HackBulgaria
PPTX
Code contract
PDF
The Ring programming language version 1.5.2 book - Part 20 of 181
PDF
The Ring programming language version 1.6 book - Part 23 of 189
ODP
Nagios Conference 2012 - Dave Josephsen - Stop Being Lazy
PDF
Tdd.eng.ver
PPTX
重構—改善既有程式的設計(chapter 9)
ES2015 promise
Async js - Nemetschek Presentaion @ HackBulgaria
Code contract
The Ring programming language version 1.5.2 book - Part 20 of 181
The Ring programming language version 1.6 book - Part 23 of 189
Nagios Conference 2012 - Dave Josephsen - Stop Being Lazy
Tdd.eng.ver
重構—改善既有程式的設計(chapter 9)

Similar to JavaScript - Promises study notes- 2019-11-30 (20)

PDF
Asynchronous JavaScript Programming with Callbacks & Promises
PDF
Promise is a Promise
PDF
Javascript Promises
PPTX
Async discussion 9_29_15
PDF
Introduction to Node JS2.pdf
PPTX
Java Script Promise
PDF
Utilizing Bluebird Promises
PDF
JavaScript Promises Simplified [Free Meetup]
PDF
Getting Comfortable with JS Promises
PDF
4 mishchevskii - testing stage18-
PDF
Promises & limbo
PDF
Promises - Asynchronous Control Flow
PPT
You promise?
PDF
Practical JavaScript Promises
PDF
Asynchronous development in JavaScript
PDF
Avoiding callback hell with promises
PDF
Kamil witecki asynchronous, yet readable, code
PPTX
Avoiding callback hell in Node js using promises
PDF
A promise is a Promise
PDF
I'm Postal for Promises in Angular
Asynchronous JavaScript Programming with Callbacks & Promises
Promise is a Promise
Javascript Promises
Async discussion 9_29_15
Introduction to Node JS2.pdf
Java Script Promise
Utilizing Bluebird Promises
JavaScript Promises Simplified [Free Meetup]
Getting Comfortable with JS Promises
4 mishchevskii - testing stage18-
Promises & limbo
Promises - Asynchronous Control Flow
You promise?
Practical JavaScript Promises
Asynchronous development in JavaScript
Avoiding callback hell with promises
Kamil witecki asynchronous, yet readable, code
Avoiding callback hell in Node js using promises
A promise is a Promise
I'm Postal for Promises in Angular
Ad

More from YangJerng Hwa (19)

PDF
Structuring Marketing Teams
PDF
Architecturing the software stack at a small business
PDF
Reactive datastore demo (2020 03-21)
PDF
2019 10-09 google ads analysis - eyeballing without proper math
PDF
2019 malaysia car accident - total loss - diy third-party claim - simplifie...
PDF
2019 09 tech publishers in malaysia
PDF
2019 09 web components
PDF
Appendix - arc of development
PDF
A Docker Diagram
PDF
A Software Problem (and a maybe-solution)
PDF
Deep learning job pitch - personal bits
PDF
Monolithic docker pattern
PPTX
What people think about Philosophers & Management Consultants
PPTX
Process for Terminating Employees in Malaysia
PPTX
Pour-over Coffee with the EK43
PDF
OTP application (with gen server child) - simple example
PDF
ERTS diagram
PPTX
Intro to Stock Trading for Programmers
PDF
A Haphazard Petcha Kutcha
Structuring Marketing Teams
Architecturing the software stack at a small business
Reactive datastore demo (2020 03-21)
2019 10-09 google ads analysis - eyeballing without proper math
2019 malaysia car accident - total loss - diy third-party claim - simplifie...
2019 09 tech publishers in malaysia
2019 09 web components
Appendix - arc of development
A Docker Diagram
A Software Problem (and a maybe-solution)
Deep learning job pitch - personal bits
Monolithic docker pattern
What people think about Philosophers & Management Consultants
Process for Terminating Employees in Malaysia
Pour-over Coffee with the EK43
OTP application (with gen server child) - simple example
ERTS diagram
Intro to Stock Trading for Programmers
A Haphazard Petcha Kutcha
Ad

Recently uploaded (20)

PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Approach and Philosophy of On baking technology
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
KodekX | Application Modernization Development
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
cuic standard and advanced reporting.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Big Data Technologies - Introduction.pptx
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Machine learning based COVID-19 study performance prediction
PDF
NewMind AI Monthly Chronicles - July 2025
The Rise and Fall of 3GPP – Time for a Sabbatical?
Mobile App Security Testing_ A Comprehensive Guide.pdf
Dropbox Q2 2025 Financial Results & Investor Presentation
Approach and Philosophy of On baking technology
Spectral efficient network and resource selection model in 5G networks
KodekX | Application Modernization Development
Chapter 3 Spatial Domain Image Processing.pdf
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Understanding_Digital_Forensics_Presentation.pptx
Digital-Transformation-Roadmap-for-Companies.pptx
cuic standard and advanced reporting.pdf
Network Security Unit 5.pdf for BCA BBA.
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Big Data Technologies - Introduction.pptx
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
NewMind AI Weekly Chronicles - August'25 Week I
Machine learning based COVID-19 study performance prediction
NewMind AI Monthly Chronicles - July 2025

JavaScript - Promises study notes- 2019-11-30

  • 1. ///
  • 2. Promises/A+ upstream from spec ● state ○ pending ○ fulfilled ○ rejected ● value ○ when fulfilled ● reason ○ When rejected ECMAScript 2020 (draft) ‘the spec’ ● [[PromiseState]] ○ pending ○ fulfilled ○ rejected ● [[PromiseResult]] ○ when fulfilled ○ when rejected FireFox downstream from spec ● <state> ○ pending ○ fulfilled ○ rejected ● <value> ○ when fulfilled ● <reason> ○ When rejected Item ● [object Promise] properties Chrome downstream from spec ● [[PromiseStatus]] ○ pending ○ resolved ○ rejected ● [[PromiseValue]] ○ when resolved ○ when rejected THIS IS THE ONLY SLIDE WHICH IS BOTH SUCCINCT AND INTERESTING
  • 3. Deciphering: ‘Promise state’ and ‘Promise status’ across Implementations ● SPEC: TC39 (2019-11 https://tc39.es/ecma262/#sec-properties-of-promise-instances ) ○ Refers to [ ‘pending’, ‘fulfilled’, ‘rejected’ ] as possible values of a (new Promise).[[PromiseState]] ○ Makes no reference to ‘status’ of a Promise ● Both: ○ (downstream) MDN (2019-11 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise ) ○ (upstream) Promises/A+ (2019-11 https://promisesaplus.com/) ■ Refer to [ ‘pending’, ‘fulfilled’, ‘rejected’ ] as ‘states’ of a Promise ■ Makes no reference to ‘status’ of a Promise ■ (At this time Firefox follows this implementation, implementing (new Promise).<state> ) ● (downstream) Chrome (2019-11 Version 78.0.3904.108 (Official Build) (64-bit) ) ○ Does not implement (new Promise).[[PromiseState]] ○ Instead, it implements (new Promise).[[PromiseStatus]], synonymously. ○ Refers to [ ‘pending’, ‘resolved’, ‘rejected’ ] as possible values of a (new Promise).[[PromiseStatus]] APPENDICE, SKIPPABLE
  • 4. Deciphering: ‘Promise result’, ‘Promise value’, and ‘Promise reason’ across Implementations ● SPEC: TC39 (2019-11 https://tc39.es/ecma262/#sec-properties-of-promise-instances ) ○ If [[PromiseState]] is ‘fulfilled’ or ‘rejected’, then the Promise’s value (informal) may be found in (new Promise).[[PromiseResult]] ● Both: ○ (downstream) MDN (2019-11 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise ) ○ (upstream) Promises/A+ (2019-11 https://promisesaplus.com/) ■ Refers to Promises ‘fulfilled’ with a ‘value’ ■ Refers to Promises ‘rejected’ with a ‘reason’ ■ Refers to both of the above, informally, as ‘results’ of a Promise ■ (At this time Firefox follows this implementation, implementing (new Promise).<value> and (new Promise).<reason>.) ● (downstream) Chrome (2019-11 Version 78.0.3904.108 (Official Build) (64-bit) ) ○ Does not implement [[PromiseResult]] ○ Instead, it implements (new Promise).[[PromiseValue]], synonymously. APPENDICE, SKIPPABLE
  • 5. ///
  • 6. new Promise ( executor_fn ) When the Promise constructor is called: 1. It creates two built-in functions (BIFs) via a subroutine. 2. Then, it calls ‘executor_fn’, passing the BIFs as arguments. The BIFs are often documented as ‘resolve_fn’ and ‘reject_fn’. But to reduce ambiguity, I recommend that you call them ‘fulfill_fn’ and ‘reject_fn’ instead. This makes their names more consistent with the names of underlying clauses in the ECMA specification. It also reduces the ambiguity of ‘resolution’, which should refer to both ‘fulfillment’ and ‘rejection’. executor_fn ( fulfill_fn@resolve_fn , reject_fn ) CreateResolvingFunctions ( promise )calls calls fulfill_fn@resolve_fn , reject_fn returns [object Promise] RejectPromise ( promise, reason ) referencing steps from FulfillPromise ( promise, value ) referencing steps from TriggerPromiseReactions ( promiseRejectionReactions, argument ) referencing steps from returns
  • 7. Deciphering: ‘Promise resolution’ and ‘Promise rejection’ across Implementations ● SPEC: TC39 (2019-11 https://tc39.es/ecma262/# ) ○ 25.6.3.1 Promise ( executor ), the (new Promise) construction makes a call to… ■ 25.6.1.3 CreateResolvingFunctions ( promise ) ● … containing the first use of the base word ‘resolve’… ● … returning ‘resolve’ and ‘reject’ as two new built-in functions ● … and is defined in terms of: ○ … the steps: 25.6.1.3.1 Promise Reject Functions, which runs 25.6.1.7 RejectPromise ( promise, reason ) which runs 25.6.1.8 TriggerPromiseReactions ( reactions, argument ) which ingests the list… ■ promise.[[PromiseRejectReactions]] (which was built by 25.6.5.4.1 PerformPromiseThen) ■ (These two clauses are consistent in their nomenclature - ‘reject, reject’.) ○ … the steps: 25.6.1.3.2 Promise Resolve Functions, which runs 25.6.1.4 FulfillPromise ( promise, value ) which runs, 25.6.1.8 TriggerPromiseReactions ( reactions, argument ) which ingests the list… ■ promise.[[PromiseFulfillReactions]] (which was built by 25.6.5.4.1 PerformPromiseThen) ■ (These two clauses are inconsistent1 in their nomenclature - ‘resolve, fulfill’.) ● So here we see another inconsistency2 , where ○ 25.6.1.3 refers to both of the following as ‘resolving functions’: ■ 25.6.1.3.1, which is itself not called ‘resolving’, and ■ 25.6.1.3.2, which is itself called ‘resolving’ ● (Comment: 25.6.1.3.2 should be called Promise Fulfill Functions, which would avoid both inconsistencies1&2 .) ■ ( … continued below … ) APPENDICE, SKIPPABLE
  • 8. ///
  • 9. p = new Promise (executor_fn) /* returns a [object Promise] */ /* Since the Promise constructor calls executor_fn, fulfill_fn and reject_fn are defined, and passed to executor_fn, by the constructor (not user); yet they are called by the user. The user defines executor_fn, and it is called before (new Promise) returns [object Promise]. Example: */ executor_fn = function ( fulfill_fn, reject_fn ) { /* State of the returned [object Promise]: 1. So long as neither fulfill_fn nor reject_fn are called: pending 2. If resolve_fn is called here: fulfilled 3. If rejected_fn is called here: rejected */ } /* There are two key methods of p which we can call: 1. p.then( onFulfilled_fn, onRejected_fn ) 2. p.catch( onRejected_fn ) */ NEXT PAGE
  • 10. NEXT PAGE /* The user defines onFulfilled_fn and onRejected_fn, but these are called by then or catch. Both parameters are optional, and each takes a single parameter. onFulfilled_fn defaults to the identity function, returning the received argument; otherwise it can be defined by the user as (fulfilmentValue => { // do something }) onRejected_fn defaults to a function, which throws the received reason; otherwise it can be defined by the user as (rejectionReason => { // do something; // rejectionReason is often but // not always an error }) */ q = p.then( onFulfilled_fn, onRejected_fn ) /* returns a [object Promise] */ r = p.catch( onRejected_fn ) /* Internally calls p.then(undefined, onRejected_fn); returns a [object Promise] */ PREVIOUS PAGE
  • 11. /* If any handler ( onFulfilled_fn or onRejected_fn ) … ● … does not return any value, then q’s returned state will be resolved, and q’s returned value will be undefined ● … returns a value, v, then q’s returned state will be resolved, and q’s returned value will be v ● … throws an error, e, then q’s returned state will be rejected, and q’s returned value will be e ● … returns a Promise, s, where ○ s is fulfilled, and s’s value is v, then q’s returned state will be fulfilled, and q’s returned value will be v ○ s is rejected, and s’s value is v, then q’s returned state will be rejected, and q’s returned value will be v ○ s is pending, then q return s */ PREVIOUS PAGE
  • 12. If [object Promise]’s state is PENDING, then ● pushes fulfillReactionPromiseReactionRecord onto the list [object Promise]. PromiseFulfillReactions ● pushes rejectReactionPromiseReactionRecord onto the list [object Promise]. PromiseRejectReactions Promise.prototype.then ( onFufill_fn, onReject_fn ) referencing steps from resultCapabilityPromiseCapabilityRecord or ‘undefined’returns If [object Promise]’s state is FULFILLED, then ● enqueues a PromiseReactionJob based on fulfillReactionPromiseReactionRecord If [object Promise]’s state is REJECTED, then ● enqueues a PromiseReactionJob based on rejectReactionPromiseReactionRecord PerformPromiseThen (promise, onFulfilled, onRejected, resultCapabilityPromiseCapabilityRecord ) ● Creates fulfillReactionPromiseReactionRecord and rejectReactionPromiseReactionRecord calls resultCapabilityPromiseCapabilityRecord returns NewPromiseCapability ( SpeciesConstructor(promise, %Promise%) ) ● … which makes a (new Promise), but lets you modify the fulfill_fn and reject_fn which are passed into the new object’s executor_fn
  • 13. Promise.resolve ( value ) ● This has a special role. If ‘value’ is a Promise or thenable, it will not be wrapped in a new Promise. referencing steps from ● If ‘value’s is an Object, and a Promise, then return ‘value’, without modification; ● elseif ‘value’s is an Object, and a thenable, but not a Promise, then attempt to coerce/convert the thenable to a Promise, and then return that Promise without modification; ● otherwise return : new Promise ( (ff,rj) => { ff (value) } ) returns PromiseResolve ( let’s not get into this) Promise.reject ( reason ) ● Returns : new Promise ( (ff,rj) => { rj (reason) } ) ● This always wraps ‘reason’ in a new rejected Promise.
  • 14. Deciphering: ‘perform promise then’, ‘promise reaction records’ ● SPEC: TC39 (2019-11 https://tc39.es/ecma262/# ) ○ 25.6.5.4 Promise.prototype.then ( onFulfilled, onRejected ) ■ … sets resultCapability = 25.6.1.5 NewPromiseCapability ( C ) ■ … returns 25.6.5.4.1 PerformPromiseThen ( promise, onFulfilled, onRejected [ , resultCapability ] ) … ● … is called by ○ 6.2.3.1 Await ○ 25.1.4.4 AsyncFromSyncIteratorContinuation ( result, promiseCapability ) ○ 25.5.3.5 AsyncGeneratorResumeNext ( generator ) ○ 25.6.5.4 Promise.prototype.then ( onFulfilled, onRejected ) ● … and (each time it is called, on the same promise) creates 25.6.1.2 PromiseReaction Records ○ … and pushes them onto ■ promise.[[PromiseFulfillReactions]], and ■ promise.[[PromiseRejectReactions]] ○ … from where they are later consumed in the next step… ● … and calls 8.4.1 EnqueueJob ( queueName, job, arguments )… ○ … which puts a 25.6.2.1 PromiseReactionJob ( reaction, argument ) into the ‘PromiseJobs’ queue APPENDICE, SKIPPABLE
  • 15. Deciphering: ‘promise capability records’ and ‘result capability’ ● SPEC: TC39 (2019-11 https://tc39.es/ecma262/# ) ○ 25.6.1.5 NewPromiseCapability ( C ) … ■ … creates 25.6.1.1 PromiseCapability Records … ● … sometimes playing the role of i.e. used as ‘result capabilities’ ■ … which are ingested by (for example)… ● 12.3.7 Import Calls , Async { Function Definitions - 14.7.11 Runtime Semantics: EvaluateBody , Arrow Function Definitions - 14.8.15 Runtime Semantics: EvaluateBody }, 25.1.4.2 The %AsyncFromSyncIteratorPrototype% Object - next(), return(), throw(), 25.5.3.6 AsyncGeneratorEnqueue ( generator, completion ), 25.6.4.1 Promise.all ( iterable ) , 25.6.4.2 Promise.allSettled ( iterable ) , 25.6.4.4 Promise.race ( iterable ) , 25.6.4.5 Promise.reject ( r ) , 25.6.4.6.1 PromiseResolve ( C, x ) , 25.6.5.4 Promise.prototype.then ( onFulfilled, onRejected ) ○ Distinct from the built-in functions ‘resolve’ and ‘reject’ created by 25.6.1.3 CreateResolvingFunctions ( promise ), ■ there also exist two UNRELATED functions ● 25.6.4.6 Promise.resolve ( x ), ○ which returns 25.6.4.6.1 PromiseResolve ( C, x ), which has a ‘flattening’ role ● 25.6.4.5 Promise.reject ( r ) , ■ both of which call NewPromiseCapability APPENDICE, SKIPPABLE
  • 16. ///
  • 17. Readings ● Promises/A+ ○ An open standard for sound, interoperable JavaScript promises. ○ This is the standard implemented below. ● Draft ECMA-262 / (November) ECMAScript® 2020 Language Specification ○ This document is the abstract specification, independent of any specific language implementation. ○ 25.6 Promise Objects ■ This is a high-level introduction to the usage of this object. ■ 25.6.3.1 Promise ( executor ) ● This is a good place to start reading, because it is generally the first call that people make to this object/class. The downstream MDN documentation introduces the reader to Promises from here. ■ 25.6.6 Properties of Promise Instances ● This is also a good place to start reading, as it addresses fundamental definitions. ● Community ○ https://stackoverflow.com/questions/29268569/what-is-the-correct-terminology-for-javascript-promises APPENDICE, SKIPPABLE
  • 18. ///