First Update:
I first thought .catch( callback)
after 'main' would return a new, pending promise of the extended Promise class, but this is incorrect - calling an async function returns a Promise
promise.
Cutting the code down further, to only produce a pending promise:
class CancellablePromise extends Promise {
constructor(executor) {
console.log("CancellablePromise::constructor");
super(executor);
}
}
async function test() {
await new CancellablePromise( ()=>null);
}
test();
shows the extended constructor being called twice in Firefox, Chrome and Node.
Now await
calls Promise.resolve
on its operand. (Edit: or it probably did in early JS engine's versions of async/await not strictly implemented to standard)
If the operand is a promise whose constructor is Promise, Promise.resolve
returns the operand unchanged.
If the operand is a thenable whose constructor is not Promise
, Promise.resolve
calls the operand's then method with both onfulfilled and onRejected handlers so as to be notified of the operand's settled state. The promise created and returned by this call to then
is of the extended class, and accounts for the second call to CancellablePromise.prototype.constructor.
Supporting evidence
new CancellablePromise().constructor
is CancellablePromise
class CancellablePromise extends Promise {
constructor(executor) {
super(executor);
}
}
console.log ( new CancellablePromise( ()=>null).constructor.name);
- Changing
CancellablePromise.prototype.constructor
to Promise
for testing purposes causes only one call to CancellablePromise
(because await
is fooled into returning its operand) :
class CancellablePromise extends Promise {
constructor(executor) {
console.log("CancellablePromise::constructor");
super(executor);
}
}
CancellablePromise.prototype.constructor = Promise; // TESTING ONLY
async function test() {
await new CancellablePromise( ()=>null);
}
test();
Second Update (with huge thanks to links provided by the OP)
Conforming Implementations
Per the await
specification
await
creates an anonymous, intermediate Promise promise with onFulilled and onRejected handlers to either
resume execution after the await
operator or throw an error from it, depending on which settled state the intermediate promise achieves.
It (await
) also calls then
on the operand promise to fulfill or reject the intermediate promise. This particular then
call returns a promise of class operandPromise.constructor
. Although the then
returned promise is never used, logging within an extended class constructor reveals the call.
If the constructor
value of an extended promise is changed back to Promise
for experimental purposes, the above then
call will silently return a Promise class promise.
Appendix: Decyphering the await
specification
Let asyncContext be the running execution context.
Let promiseCapability be ! NewPromiseCapability(%Promise%).
Creates a new jQuery-like deferred object with promise
, resolve
and reject
properties, calling it a "PromiseCapability Record" instead. The deferred's promise
object is of the (global) base Promise constructor class.
- Perform ! Call(promiseCapability.[[Resolve]], undefined, « promise »).
Resolve the deferred promise with the right operand of await
. The resolution process either calls the then
method of the operand if it is a "thenable", or fulfills the deferred promise if the operand is some other, non-promise, value.
Let stepsFulfilled be the algorithm steps defined in Await Fulfilled Functions.
Let onFulfilled be CreateBuiltinFunction(stepsFulfilled, « [[AsyncContext]] »).
Set onFulfilled.[[AsyncContext]] to asyncContext.
Create an onfulfilled handler to resume the await
operation, inside the async
function it was called in, by returning the fulfilled value of the operand passed as argument to the handler.
Let stepsRejected be the algorithm steps defined in Await Rejected Functions.
Let onRejected be CreateBuiltinFunction(stepsRejected, « [[AsyncContext]] »).
Set onRejected.[[AsyncContext]] to asyncContext.
Create an onrejected handler to resume the await
operation, inside the async
function it was called in, by throwing a promise rejection reason passed to the handler as its argument.
- Perform ! PerformPromiseThen(promiseCapability.[[Promise]], onFulfilled, onRejected).
Call then
on the deferred promise with these two handlers so that await
can respond to its operand being settled.
This call using three parameters is an optimisation that effectively means then
has been called internally and won't be creating or returning a promise from the call. Hence settlement of the deferred will dispatch calling one of its settlement handlers to the promise job queue for execution, but has no additional side effects.
Remove asyncContext from the execution context stack and restore the execution context that is at the top of the execution context stack as the running execution context.
Set the code evaluation state of asyncContext such that when evaluation is resumed with a Completion completion, the following steps of the algorithm that invoked Await will be performed, with completion available.
Store where to resume after a successful await
and return to the event loop or micro task queue manager.
console.log("CancellablePromise::constructor");
addconsole.log(executor+'')
- and all will be revealedexecutor
, not mine:function () { [native code] }
await
do?.then
- but the internals ofasync
/await
are not important - remove theawait
(but keep theasync
) and while now the code isn't behaving correctly, you'll see only a singleCancellablePromise::constructor
callconsole.trace("Trace")
in the constructor to see the stack when it's called. You will see the first call made inmain
, but the next inPromise.then()
suggesting that await is calling the constructor.