When the finally
method is called with argument onFinally, the following steps are taken:
- Let promise be the this value.
- If Type(promise) is not Object, throw a TypeError exception.
- Assert: IsConstructor(C) is true.
1. If IsCallable(onFinally) is false,
- Let thenFinally be onFinally.
- Let catchFinally be onFinally. 1. Else,
- Let thenFinally be a new built-in function object as defined in ThenFinally Function.
- Let catchFinally be a new built-in function object as defined in CatchFinally Function.
- Set thenFinally and catchFinally's [[Constructor]] internal slots to C.
- Set thenFinally and catchFinally's [[OnFinally]] internal slots to onFinally.
- Return ? Invoke(promise, "then", « thenFinally, catchFinally »).
A ThenFinally function is an anonymous built-in function that has a [[Constructor]] and an [[OnFinally]] internal slot. The value of the [[Constructor]] internal slot is a Promise
-like constructor function object, and the value of the [[OnFinally]] internal slot is a function object.
When a ThenFinally function F is called with argument value, the following steps are taken:
- Let onFinally be F.[[OnFinally]].
- Assert: IsCallable(onFinally) is true.
- Let result be ? Call(onFinally, undefined).
- Let C be F.[[Constructor]].
- Assert: IsConstructor(C) is true.
- Let promise be ? PromiseResolve(C, result).
- Let valueThunk be equivalent to a function that returns value.
- Return ? Invoke(promise,
"then"
, « valueThunk »).
A CatchFinally function is an anonymous built-in function that has a [[Constructor]] and an [[OnFinally]] internal slot. The value of the [[Constructor]] internal slot is a Promise
-like constructor function object, and the value of the [[OnFinally]] internal slot is a function object.
When a CatchFinally function F is called with argument reason, the following steps are taken:
- Let onFinally be F.[[OnFinally]].
- Assert: IsCallable(onFinally) is true.
- Let result be ? Call(onFinally, undefined).
- Let C be F.[[Constructor]].
- Assert: IsConstructor(C) is true.
- Let promise be ? PromiseResolve(C, result).
- Let thrower be equivalent to a function that throws reason.
- Return ? Invoke(promise,
"then"
, « thrower »).
The resolve
function returns either a new promise resolved with the passed argument, or the argument itself if the argument is a promise produced by this constructor.
- Let C be the this value.
- If Type(C) is not Object, throw a TypeError exception.
- Return ? PromiseResolve(C, x).
Note: the resolve
function expects its this value to be a constructor function that supports the parameter conventions of the Promise
constructor.
The abstract operation PromiseResolve, given a constructor and a value, returns a new promise resolved with that value.
- Assert: Type(C) is Object.
- If IsPromise(x) is true, then
1. Let xConstructor be ? Get(x,
"constructor"
). 1. If SameValue(xConstructor, C) is true, return x. - Let promiseCapability be ? NewPromiseCapability(C).
- Perform ? Call(promiseCapability.[[Resolve]], undefined, « x »).
- Return promiseCapability.[[Promise]].