Skip to content

Commit

Permalink
Editorial: use abstract closure in Promise.prototype.finally (tc39#2439)
Browse files Browse the repository at this point in the history
  • Loading branch information
bakkot authored and mathiasbynens committed Oct 18, 2021
1 parent 955837d commit 51f665e
Showing 1 changed file with 17 additions and 47 deletions.
64 changes: 17 additions & 47 deletions spec.html
Original file line number Diff line number Diff line change
Expand Up @@ -39236,7 +39236,7 @@ <h1>Promise.prototype.constructor</h1>
<p>The initial value of `Promise.prototype.constructor` is %Promise%.</p>
</emu-clause>

<emu-clause id="sec-promise.prototype.finally">
<emu-clause id="sec-promise.prototype.finally" oldids="sec-thenfinallyfunctions,sec-catchfinallyfunctions">
<h1>Promise.prototype.finally ( _onFinally_ )</h1>
<p>When the `finally` method is called with argument _onFinally_, the following steps are taken:</p>
<emu-alg>
Expand All @@ -39248,54 +39248,24 @@ <h1>Promise.prototype.finally ( _onFinally_ )</h1>
1. Let _thenFinally_ be _onFinally_.
1. Let _catchFinally_ be _onFinally_.
1. Else,
1. Let _stepsThenFinally_ be the algorithm steps defined in <emu-xref href="#sec-thenfinallyfunctions" title></emu-xref>.
1. Let _lengthThenFinally_ be the number of non-optional parameters of the function definition in <emu-xref href="#sec-thenfinallyfunctions" title></emu-xref>.
1. Let _thenFinally_ be ! CreateBuiltinFunction(_stepsThenFinally_, _lengthThenFinally_, *""*, &laquo; [[Constructor]], [[OnFinally]] &raquo;).
1. Set _thenFinally_.[[Constructor]] to _C_.
1. Set _thenFinally_.[[OnFinally]] to _onFinally_.
1. Let _stepsCatchFinally_ be the algorithm steps defined in <emu-xref href="#sec-catchfinallyfunctions" title></emu-xref>.
1. Let _lengthCatchFinally_ be the number of non-optional parameters of the function definition in <emu-xref href="#sec-catchfinallyfunctions" title></emu-xref>.
1. Let _catchFinally_ be ! CreateBuiltinFunction(_stepsCatchFinally_, _lengthCatchFinally_, *""*, &laquo; [[Constructor]], [[OnFinally]] &raquo;).
1. Set _catchFinally_.[[Constructor]] to _C_.
1. Set _catchFinally_.[[OnFinally]] to _onFinally_.
1. Let _thenFinallyClosure_ be a new Abstract Closure with parameters (_value_) that captures _onFinally_ and _C_ and performs the following steps when called:
1. Let _result_ be ? Call(_onFinally_, *undefined*).
1. Let _promise_ be ? PromiseResolve(_C_, _result_).
1. Let _returnValue_ be a new Abstract Closure with no parameters that captures _value_ and performs the following steps when called:
1. Return _value_.
1. Let _valueThunk_ be ! CreateBuiltinFunction(_returnValue_, 0, *""*, &laquo; &raquo;).
1. Return ? Invoke(_promise_, *"then"*, &laquo; _valueThunk_ &raquo;).
1. Let _thenFinally_ be ! CreateBuiltinFunction(_thenFinallyClosure_, 1, *""*, &laquo; &raquo;).
1. Let _catchFinallyClosure_ be a new Abstract Closure with parameters (_reason_) that captures _onFinally_ and _C_ and performs the following steps when called:
1. Let _result_ be ? Call(_onFinally_, *undefined*).
1. Let _promise_ be ? PromiseResolve(_C_, _result_).
1. Let _throwReason_ be a new Abstract Closure with no parameters that captures _reason_ and performs the following steps when called:
1. Return ThrowCompletion(_reason_).
1. Let _thrower_ be ! CreateBuiltinFunction(_throwReason_, 0, *""*, &laquo; &raquo;).
1. Return ? Invoke(_promise_, *"then"*, &laquo; _thrower_ &raquo;).
1. Let _catchFinally_ be ! CreateBuiltinFunction(_catchFinallyClosure_, 1, *""*, &laquo; &raquo;).
1. Return ? Invoke(_promise_, *"then"*, &laquo; _thenFinally_, _catchFinally_ &raquo;).
</emu-alg>

<emu-clause id="sec-thenfinallyfunctions">
<h1>Then Finally Functions</h1>
<p>A Then Finally 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.</p>
<p>When a Then Finally function is called with argument _value_, the following steps are taken:</p>
<emu-alg>
1. Let _F_ be the active function object.
1. Let _onFinally_ be _F_.[[OnFinally]].
1. Assert: IsCallable(_onFinally_) is *true*.
1. Let _result_ be ? Call(_onFinally_, *undefined*).
1. Let _C_ be _F_.[[Constructor]].
1. Assert: IsConstructor(_C_) is *true*.
1. Let _promise_ be ? PromiseResolve(_C_, _result_).
1. Let _valueThunk_ be equivalent to a function that returns _value_.
1. Return ? Invoke(_promise_, *"then"*, &laquo; _valueThunk_ &raquo;).
</emu-alg>
<p>The *"length"* property of a Then Finally function is *1*<sub>𝔽</sub>.</p>
</emu-clause>

<emu-clause id="sec-catchfinallyfunctions">
<h1>Catch Finally Functions</h1>
<p>A Catch Finally 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.</p>
<p>When a Catch Finally function is called with argument _reason_, the following steps are taken:</p>
<emu-alg>
1. Let _F_ be the active function object.
1. Let _onFinally_ be _F_.[[OnFinally]].
1. Assert: IsCallable(_onFinally_) is *true*.
1. Let _result_ be ? Call(_onFinally_, *undefined*).
1. Let _C_ be _F_.[[Constructor]].
1. Assert: IsConstructor(_C_) is *true*.
1. Let _promise_ be ? PromiseResolve(_C_, _result_).
1. Let _thrower_ be equivalent to a function that throws _reason_.
1. Return ? Invoke(_promise_, *"then"*, &laquo; _thrower_ &raquo;).
</emu-alg>
<p>The *"length"* property of a Catch Finally function is *1*<sub>𝔽</sub>.</p>
</emu-clause>
</emu-clause>

<emu-clause id="sec-promise.prototype.then">
Expand Down

0 comments on commit 51f665e

Please sign in to comment.