ECMA-262 (12th Edition) ECMAScript 2021 Language Specification - page 58

 

  Главная      Manuals     ECMA-262 (12th Edition) ECMAScript 2021 Language Specification

 

Search            copyright infringement  

 

 

 

 

 

 

 

 

 

 

 

Content      ..     56      57      58      59     ..

 

 

 

ECMA-262 (12th Edition) ECMAScript 2021 Language Specification - page 58

 

 

6.  Let 

values

 be 

F

.[[Values]].

7.  Let 

promiseCapability

 be 

F

.[[Capability]].

8.  Let 

remainingElementsCount

 be 

F

.[[RemainingElements]].

9.  Let 

obj

 be ! 

OrdinaryObjectCreate

(

%Object.prototype%

).

10.  Perform ! 

CreateDataPropertyOrThrow

(

obj

"status"

"rejected"

).

11.  Perform ! 

CreateDataPropertyOrThrow

(

obj

"reason"

x

).

12.  Set 

values

[

index

] to 

obj

.

13.  Set 

remainingElementsCount

.[[Value]] to 

remainingElementsCount

.[[Value]] - 1.

14.  If 

remainingElementsCount

.[[Value]] is 0, then

a.  Let 

valuesArray

 be ! 

CreateArrayFromList

(

values

).

b.  Return ? 

Call

(

promiseCapability

.[[Resolve]], 

undefined

, « 

valuesArray

 »).

15.  Return 

undefined

.

The 

"length"

 property of a 

Promise.allSettled

Promise.allSettled

 reject element function is 

1

𝔽

.

The 

any

any

 function returns a promise that is fulfilled by the first given promise to be fulfilled, or rejected with an

AggregateError

AggregateError

 holding the rejection reasons if all of the given promises are rejected. It resolves all elements of

the passed iterable to promises as it runs this algorithm.

1.  Let 

C

 be the 

this

 value.

2.  Let 

promiseCapability

 be ? 

NewPromiseCapability

(

C

).

3.  Let 

promiseResolve

 be 

GetPromiseResolve

(

C

).

4. 

IfAbruptRejectPromise

(

promiseResolve

promiseCapability

).

5.  Let 

iteratorRecord

 be 

GetIterator

(

iterable

).

6. 

IfAbruptRejectPromise

(

iteratorRecord

promiseCapability

).

7.  Let 

result

 be 

PerformPromiseAny

(

iteratorRecord

C

promiseCapability

promiseResolve

).

8.  If 

result

 is an 

abrupt completion

, then

a.  If 

iteratorRecord

.[[Done]] is 

false

, set 

result

 to 

IteratorClose

(

iteratorRecord

result

).

b. 

IfAbruptRejectPromise

(

result

promiseCapability

).

9.  Return 

Completion

(

result

).

NOTE

The abstract operation PerformPromiseAny takes arguments 

iteratorRecord

constructor

resultCapability

 (a

PromiseCapability Record

), and 

promiseResolve

. It performs the following steps when called:

1. 

Assert

: ! 

IsConstructor

(

constructor

) is 

true

.

2. 

Assert

: ! 

IsCallable

(

promiseResolve

) is 

true

.

3.  Let 

errors

 be a new empty 

List

.

4.  Let 

remainingElementsCount

 be the 

Record

 { [[Value]]: 1 }.

5.  Let 

index

 be 0.

6.  Repeat,

a.  Let 

next

 be 

IteratorStep

(

iteratorRecord

).

The 

any

any

 function requires its 

this

 value to be a 

constructor

 function that supports the parameter

conventions of the 

Promise

Promise

 

constructor

.

27.2.4.3  Promise.any ( 

iterable

 )

27.2.4.3.1  PerformPromiseAny ( 

iteratorRecord

constructor

resultCapability

promiseResolve

 )

768

b.  If 

next

 is an 

abrupt completion

, set 

iteratorRecord

.[[Done]] to 

true

.

c. 

ReturnIfAbrupt

(

next

).

d.  If 

next

 is 

false

, then

i.  Set 

iteratorRecord

.[[Done]] to 

true

.

ii.  Set 

remainingElementsCount

.[[Value]] to 

remainingElementsCount

.[[Value]] - 1.

iii.  If 

remainingElementsCount

.[[Value]] is 0, then

1.  Let 

error

 be a newly created 

AggregateError

AggregateError

 object.

2.  Perform ! 

DefinePropertyOrThrow

(

error

"errors"

, PropertyDescriptor { [[Configurable]]:

true

, [[Enumerable]]: 

false

, [[Writable]]: 

true

, [[Value]]: ! 

CreateArrayFromList

(

errors

) }).

3.  Return 

ThrowCompletion

(

error

).

iv.  Return 

resultCapability

.[[Promise]].

e.  Let 

nextValue

 be 

IteratorValue

(

next

).

f.  If 

nextValue

 is an 

abrupt completion

, set 

iteratorRecord

.[[Done]] to 

true

.

g. 

ReturnIfAbrupt

(

nextValue

).

h.  Append 

undefined

 to 

errors

.

i.  Let 

nextPromise

 be ? 

Call

(

promiseResolve

constructor

, « 

nextValue

 »).

j.  Let 

stepsRejected

 be the algorithm steps defined in 

Promise.any

Promise.any

 Reject Element Functions

.

k.  Let 

lengthRejected

 be the number of non-optional parameters of the function definition in

Promise.any

Promise.any

 Reject Element Functions

.

l.  Let 

onRejected

 be ! 

CreateBuiltinFunction

(

stepsRejected

lengthRejected

""

, « [[AlreadyCalled]], [[Index]],

[[Errors]], [[Capability]], [[RemainingElements]] »).

m.  Set 

onRejected

.[[AlreadyCalled]] to 

false

.

n.  Set 

onRejected

.[[Index]] to 

index

.

o.  Set 

onRejected

.[[Errors]] to 

errors

.

p.  Set 

onRejected

.[[Capability]] to 

resultCapability

.

q.  Set 

onRejected

.[[RemainingElements]] to 

remainingElementsCount

.

r.  Set 

remainingElementsCount

.[[Value]] to 

remainingElementsCount

.[[Value]] + 1.

s.  Perform ? 

Invoke

(

nextPromise

"then"

, « 

resultCapability

.[[Resolve]], 

onRejected

 »).

t.  Set 

index

 to 

index

 + 1.

Promise.any

Promise.any

 reject element function is an anonymous built-in function that is used to reject a specific

Promise.any

Promise.any

 element. Each 

Promise.any

Promise.any

 reject element function has [[Index]], [[Errors]], [[Capability]],

[[RemainingElements]], and [[AlreadyCalled]] internal slots.

When a 

Promise.any

Promise.any

 reject element function is called with argument 

x

, the following steps are taken:

1.  Let 

F

 be the 

active function object

.

2.  If 

F

.[[AlreadyCalled]] is 

true

, return 

undefined

.

3.  Set 

F

.[[AlreadyCalled]] to 

true

.

4.  Let 

index

 be 

F

.[[Index]].

5.  Let 

errors

 be 

F

.[[Errors]].

6.  Let 

promiseCapability

 be 

F

.[[Capability]].

7.  Let 

remainingElementsCount

 be 

F

.[[RemainingElements]].

8.  Set 

errors

[

index

] to 

x

.

9.  Set 

remainingElementsCount

.[[Value]] to 

remainingElementsCount

.[[Value]] - 1.

10.  If 

remainingElementsCount

.[[Value]] is 0, then

27.2.4.3.2  

Promise.any

Promise.any

 Reject Element Functions

769

a.  Let 

error

 be a newly created 

AggregateError

AggregateError

 object.

b.  Perform ! 

DefinePropertyOrThrow

(

error

"errors"

, PropertyDescriptor { [[Configurable]]: 

true

,

[[Enumerable]]: 

false

, [[Writable]]: 

true

, [[Value]]: ! 

CreateArrayFromList

(

errors

) }).

c.  Return ? 

Call

(

promiseCapability

.[[Reject]], 

undefined

, « 

error

 »).

11.  Return 

undefined

.

The 

"length"

 property of a 

Promise.any

Promise.any

 reject element function is 

1

𝔽

.

The initial value of 

Promise.prototype

Promise.prototype

 is the 

Promise prototype object

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

The 

race

race

 function returns a new promise which is settled in the same way as the first passed promise to settle. It

resolves all elements of the passed 

iterable

 to promises as it runs this algorithm.

1.  Let 

C

 be the 

this

 value.

2.  Let 

promiseCapability

 be ? 

NewPromiseCapability

(

C

).

3.  Let 

promiseResolve

 be 

GetPromiseResolve

(

C

).

4. 

IfAbruptRejectPromise

(

promiseResolve

promiseCapability

).

5.  Let 

iteratorRecord

 be 

GetIterator

(

iterable

).

6. 

IfAbruptRejectPromise

(

iteratorRecord

promiseCapability

).

7.  Let 

result

 be 

PerformPromiseRace

(

iteratorRecord

C

promiseCapability

promiseResolve

).

8.  If 

result

 is an 

abrupt completion

, then

a.  If 

iteratorRecord

.[[Done]] is 

false

, set 

result

 to 

IteratorClose

(

iteratorRecord

result

).

b. 

IfAbruptRejectPromise

(

result

promiseCapability

).

9.  Return 

Completion

(

result

).

NOTE 1

NOTE 2

The abstract operation PerformPromiseRace takes arguments 

iteratorRecord

constructor

resultCapability

 (a

PromiseCapability Record

), and 

promiseResolve

. It performs the following steps when called:

1. 

Assert

IsConstructor

(

constructor

) is 

true

.

2. 

Assert

IsCallable

(

promiseResolve

) is 

true

.

3.  Repeat,

a.  Let 

next

 be 

IteratorStep

(

iteratorRecord

).

b.  If 

next

 is an 

abrupt completion

, set 

iteratorRecord

.[[Done]] to 

true

.

If the 

iterable

 argument is empty or if none of the promises in 

iterable

 ever settle then the pending

promise returned by this method will never be settled.

The 

race

race

 function expects its 

this

 value to be a 

constructor

 function that supports the parameter

conventions of the Promise 

constructor

. It also expects that its 

this

 value provides a 

resolve

resolve

method.

27.2.4.4  Promise.prototype

27.2.4.5  Promise.race ( 

iterable

 )

27.2.4.5.1  PerformPromiseRace ( 

iteratorRecord

constructor

resultCapability

promiseResolve

 )

770

c. 

ReturnIfAbrupt

(

next

).

d.  If 

next

 is 

false

, then

i.  Set 

iteratorRecord

.[[Done]] to 

true

.

ii.  Return 

resultCapability

.[[Promise]].

e.  Let 

nextValue

 be 

IteratorValue

(

next

).

f.  If 

nextValue

 is an 

abrupt completion

, set 

iteratorRecord

.[[Done]] to 

true

.

g. 

ReturnIfAbrupt

(

nextValue

).

h.  Let 

nextPromise

 be ? 

Call

(

promiseResolve

constructor

, « 

nextValue

 »).

i.  Perform ? 

Invoke

(

nextPromise

"then"

, « 

resultCapability

.[[Resolve]], 

resultCapability

.[[Reject]] »).

The 

reject

reject

 function returns a new promise rejected with the passed argument.

1.  Let 

C

 be the 

this

 value.

2.  Let 

promiseCapability

 be ? 

NewPromiseCapability

(

C

).

3.  Perform ? 

Call

(

promiseCapability

.[[Reject]], 

undefined

, « 

r

 »).

4.  Return 

promiseCapability

.[[Promise]].

NOTE

The 

resolve

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

.

1.  Let 

C

 be the 

this

 value.

2.  If 

Type

(

C

) is not Object, throw a 

TypeError

 exception.

3.  Return ? 

PromiseResolve

(

C

x

).

NOTE

The abstract operation PromiseResolve takes arguments 

C

 (a 

constructor

) and 

x

 (an 

ECMAScript language value

). It

returns a new promise resolved with 

x

. It performs the following steps when called:

1. 

Assert

Type

(

C

) is Object.

2.  If 

IsPromise

(

x

) is 

true

, then

a.  Let 

xConstructor

 be ? 

Get

(

x

"constructor"

).

b.  If 

SameValue

(

xConstructor

C

) is 

true

, return 

x

.

3.  Let 

promiseCapability

 be ? 

NewPromiseCapability

(

C

).

4.  Perform ? 

Call

(

promiseCapability

.[[Resolve]], 

undefined

, « 

x

 »).

5.  Return 

promiseCapability

.[[Promise]].

The 

reject

reject

 function expects its 

this

 value to be a 

constructor

 function that supports the

parameter conventions of the Promise 

constructor

.

The 

resolve

resolve

 function expects its 

this

 value to be a 

constructor

 function that supports the

parameter conventions of the Promise 

constructor

.

27.2.4.6  Promise.reject ( 

r

 )

27.2.4.7  Promise.resolve ( 

x

 )

27.2.4.7.1  PromiseResolve ( 

C

x

 )

771

Promise[@@species]

Promise[@@species]

 is an 

accessor property

 whose set accessor function is 

undefined

. Its get accessor function

performs the following steps:

1.  Return the 

this

 value.

The value of the 

"name"

 property of this function is 

"get [Symbol.species]"

.

NOTE

The 

Promise prototype object

:

is 

%Promise.prototype%

.

has a [[Prototype]] internal slot whose value is 

%Object.prototype%

.

is an 

ordinary object

.

does not have a [[PromiseState]] internal slot or any of the other internal slots of Promise instances.

When the 

catch

catch

 method is called with argument 

onRejected

, the following steps are taken:

1.  Let 

promise

 be the 

this

 value.

2.  Return ? 

Invoke

(

promise

"then"

, « 

undefined

onRejected

 »).

The initial value of 

Promise.prototype.constructor

Promise.prototype.constructor

 is 

%Promise%

.

When the 

finally

finally

 method is called with argument 

onFinally

, the following steps are taken:

1.  Let 

promise

 be the 

this

 value.

2.  If 

Type

(

promise

) is not Object, throw a 

TypeError

 exception.

3.  Let 

C

 be ? 

SpeciesConstructor

(

promise

%Promise%

).

4. 

Assert

IsConstructor

(

C

) is 

true

.

5.  If 

IsCallable

(

onFinally

) is 

false

, then

a.  Let 

thenFinally

 be 

onFinally

.

b.  Let 

catchFinally

 be 

onFinally

.

6.  Else,

a.  Let 

stepsThenFinally

 be the algorithm steps defined in 

Then Finally Functions

.

b.  Let 

lengthThenFinally

 be the number of non-optional parameters of the function definition in 

Then

Finally Functions

.

c.  Let 

thenFinally

 be ! 

CreateBuiltinFunction

(

stepsThenFinally

lengthThenFinally

""

, « [[Constructor]],

Promise prototype methods normally use their 

this

 value's 

constructor

 to create a derived object.

However, a subclass 

constructor

 may over-ride that default behaviour by redefining its

@@species

 property.

27.2.4.8  get Promise [ @@species ]

27.2.5  Properties of the Promise Prototype Object

27.2.5.1  Promise.prototype.catch ( 

onRejected

 )

27.2.5.2  Promise.prototype.constructor

27.2.5.3  Promise.prototype.finally ( 

onFinally

 )

772

[[OnFinally]] »).

d.  Set 

thenFinally

.[[Constructor]] to 

C

.

e.  Set 

thenFinally

.[[OnFinally]] to 

onFinally

.

f.  Let 

stepsCatchFinally

 be the algorithm steps defined in 

Catch Finally Functions

.

g.  Let 

lengthCatchFinally

 be the number of non-optional parameters of the function definition in 

Catch

Finally Functions

.

h.  Let 

catchFinally

 be ! 

CreateBuiltinFunction

(

stepsCatchFinally

lengthCatchFinally

""

, « [[Constructor]],

[[OnFinally]] »).

i.  Set 

catchFinally

.[[Constructor]] to 

C

.

j.  Set 

catchFinally

.[[OnFinally]] to 

onFinally

.

7.  Return ? 

Invoke

(

promise

"then"

, « 

thenFinally

catchFinally

 »).

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

Promise

-like 

constructor

 

function object

, and the value of the

[[OnFinally]] internal slot is a 

function object

.

When a Then Finally function is called with argument 

value

, the following steps are taken:

1.  Let 

F

 be the 

active function object

.

2.  Let 

onFinally

 be 

F

.[[OnFinally]].

3. 

Assert

IsCallable

(

onFinally

) is 

true

.

4.  Let 

result

 be ? 

Call

(

onFinally

undefined

).

5.  Let 

C

 be 

F

.[[Constructor]].

6. 

Assert

IsConstructor

(

C

) is 

true

.

7.  Let 

promise

 be ? 

PromiseResolve

(

C

result

).

8.  Let 

valueThunk

 be equivalent to a function that returns 

value

.

9.  Return ? 

Invoke

(

promise

"then"

, « 

valueThunk

 »).

The 

"length"

 property of a Then Finally function is 

1

𝔽

.

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

Promise

-like 

constructor

 

function object

, and the value of the

[[OnFinally]] internal slot is a 

function object

.

When a Catch Finally function is called with argument 

reason

, the following steps are taken:

1.  Let 

F

 be the 

active function object

.

2.  Let 

onFinally

 be 

F

.[[OnFinally]].

3. 

Assert

IsCallable

(

onFinally

) is 

true

.

4.  Let 

result

 be ? 

Call

(

onFinally

undefined

).

5.  Let 

C

 be 

F

.[[Constructor]].

6. 

Assert

IsConstructor

(

C

) is 

true

.

7.  Let 

promise

 be ? 

PromiseResolve

(

C

result

).

8.  Let 

thrower

 be equivalent to a function that throws 

reason

.

9.  Return ? 

Invoke

(

promise

"then"

, « 

thrower

 »).

27.2.5.3.1  Then Finally Functions

27.2.5.3.2  Catch Finally Functions

773

The 

"length"

 property of a Catch Finally function is 

1

𝔽

.

When the 

then

then

 method is called with arguments 

onFulfilled

 and 

onRejected

, the following steps are taken:

1.  Let 

promise

 be the 

this

 value.

2.  If 

IsPromise

(

promise

) is 

false

, throw a 

TypeError

 exception.

3.  Let 

C

 be ? 

SpeciesConstructor

(

promise

%Promise%

).

4.  Let 

resultCapability

 be ? 

NewPromiseCapability

(

C

).

5.  Return 

PerformPromiseThen

(

promise

onFulfilled

onRejected

resultCapability

).

The abstract operation PerformPromiseThen takes arguments 

promise

onFulfilled

, and 

onRejected

 and optional

argument 

resultCapability

 (a 

PromiseCapability Record

). It performs the “then” operation on 

promise

 using 

onFulfilled

and 

onRejected

 as its settlement actions. If 

resultCapability

 is passed, the result is stored by updating 

resultCapability

's

promise. If it is not passed, then PerformPromiseThen is being called by a specification-internal operation where the
result does not matter. It performs the following steps when called:

1. 

Assert

IsPromise

(

promise

) is 

true

.

2.  If 

resultCapability

 is not present, then

a.  Set 

resultCapability

 to 

undefined

.

3.  If 

IsCallable

(

onFulfilled

) is 

false

, then

a.  Let 

onFulfilledJobCallback

 be 

empty

.

4.  Else,

a.  Let 

onFulfilledJobCallback

 be 

HostMakeJobCallback

(

onFulfilled

).

5.  If 

IsCallable

(

onRejected

) is 

false

, then

a.  Let 

onRejectedJobCallback

 be 

empty

.

6.  Else,

a.  Let 

onRejectedJobCallback

 be 

HostMakeJobCallback

(

onRejected

).

7.  Let 

fulfillReaction

 be the PromiseReaction { [[Capability]]: 

resultCapability

, [[Type]]: 

Fulfill

, [[Handler]]:

onFulfilledJobCallback

 }.

8.  Let 

rejectReaction

 be the PromiseReaction { [[Capability]]: 

resultCapability

, [[Type]]: 

Reject

, [[Handler]]:

onRejectedJobCallback

 }.

9.  If 

promise

.[[PromiseState]] is 

pending

, then

a.  Append 

fulfillReaction

 as the last element of the 

List

 that is 

promise

.[[PromiseFulfillReactions]].

b.  Append 

rejectReaction

 as the last element of the 

List

 that is 

promise

.[[PromiseRejectReactions]].

10.  Else if 

promise

.[[PromiseState]] is 

fulfilled

, then

a.  Let 

value

 be 

promise

.[[PromiseResult]].

b.  Let 

fulfillJob

 be 

NewPromiseReactionJob

(

fulfillReaction

value

).

c.  Perform 

HostEnqueuePromiseJob

(

fulfillJob

.[[Job]], 

fulfillJob

.[[Realm]]).

11.  Else,

a. 

Assert

: The value of 

promise

.[[PromiseState]] is 

rejected

.

b.  Let 

reason

 be 

promise

.[[PromiseResult]].

c.  If 

promise

.[[PromiseIsHandled]] is 

false

, perform 

HostPromiseRejectionTracker

(

promise

"handle"

).

d.  Let 

rejectJob

 be 

NewPromiseReactionJob

(

rejectReaction

reason

).

e.  Perform 

HostEnqueuePromiseJob

(

rejectJob

.[[Job]], 

rejectJob

.[[Realm]]).

27.2.5.4  Promise.prototype.then ( 

onFulfilled

onRejected

 )

27.2.5.4.1  PerformPromiseThen ( 

promise

onFulfilled

onRejected

 [ , 

resultCapability

 ] )

774

12.  Set 

promise

.[[PromiseIsHandled]] to 

true

.

13.  If 

resultCapability

 is 

undefined

, then

a.  Return 

undefined

.

14.  Else,

a.  Return 

resultCapability

.[[Promise]].

The initial value of the 

@@toStringTag

 property is the String value 

"Promise"

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

Promise instances are ordinary objects that inherit properties from the 

Promise prototype object

 (the intrinsic,

%Promise.prototype%

). Promise instances are initially created with the internal slots described in 

Table 72

.

Table 72: Internal Slots of Promise Instances

Internal Slot

Description

[[PromiseState]]

One of 

pending

fulfilled

, or 

rejected

. Governs how a promise will react to incoming

calls to its 

then

then

 method.

[[PromiseResult]]

The value with which the promise has been fulfilled or rejected, if any. Only
meaningful if [[PromiseState]] is not 

pending

.

[[PromiseFulfillReactions]] A 

List

 of PromiseReaction records to be processed when/if the promise transitions

from the 

pending

 state to the 

fulfilled

 state.

[[PromiseRejectReactions]] A 

List

 of PromiseReaction records to be processed when/if the promise transitions

from the 

pending

 state to the 

rejected

 state.

[[PromiseIsHandled]]

A boolean indicating whether the promise has ever had a fulfillment or rejection
handler; used in unhandled rejection tracking.

GeneratorFunction objects are functions that are usually created by evaluating 

GeneratorDeclaration

s, 

GeneratorExpression

s, and 

GeneratorMethod

s. They may also be created by calling the 

%GeneratorFunction%

 intrinsic.

27.2.5.5  Promise.prototype [ @@toStringTag ]

27.2.6  Properties of Promise Instances

27.3  GeneratorFunction Objects

775

Figure 5 (Informative): Generator Objects Relationships

The GeneratorFunction 

constructor

:

is 

%GeneratorFunction%

.

is a subclass of 

Function

Function

.

creates and initializes a new GeneratorFunction object when called as a function rather than as a 

constructor

.

Thus the function call 

GeneratorFunction (…)

GeneratorFunction (…)

 is equivalent to the object creation expression

new GeneratorFunction (…)

new GeneratorFunction (…)

 with the same arguments.

is designed to be subclassable. It may be used as the value of an 

extends

extends

 clause of a class definition. Subclass

constructors that intend to inherit the specified GeneratorFunction behaviour must include a 

super

super

 call to the

27.3.1  The GeneratorFunction Constructor

776

GeneratorFunction 

constructor

 to create and initialize subclass instances with the internal slots necessary for

built-in GeneratorFunction behaviour. All ECMAScript syntactic forms for defining generator function objects
create direct instances of GeneratorFunction. There is no syntactic means to create instances of
GeneratorFunction subclasses.

The last argument specifies the body (executable code) of a generator function; any preceding arguments specify
formal parameters.

When the 

GeneratorFunction

GeneratorFunction

 function is called with some arguments 

p1

p2

, … , 

pn

body

 (where 

n

 might be 0,

that is, there are no “

p

” arguments, and where 

body

 might also not be provided), the following steps are taken:

1.  Let 

C

 be the 

active function object

.

2.  Let 

args

 be the 

argumentsList

 that was passed to this function by [[Call]] or [[Construct]].

3.  Return ? 

CreateDynamicFunction

(

C

, NewTarget, 

generator

args

).

NOTE

The GeneratorFunction 

constructor

:

is a standard built-in 

function object

 that inherits from the Function 

constructor

.

has a [[Prototype]] internal slot whose value is 

%Function%

.

has a 

"name"

 property whose value is 

"GeneratorFunction"

.

has the following properties:

This is a 

data property

 with a value of 1. This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

,

[[Configurable]]: 

true

 }.

The initial value of 

GeneratorFunction.prototype

GeneratorFunction.prototype

 is the 

GeneratorFunction prototype object

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

The 

GeneratorFunction prototype object

:

is 

%GeneratorFunction.prototype%

 (see 

Figure 5

).

is an 

ordinary object

.

is not a 

function object

 and does not have an [[ECMAScriptCode]] internal slot or any other of the internal slots

listed in 

Table 29

 or 

Table 73

.

has a [[Prototype]] internal slot whose value is 

%Function.prototype%

.

See NOTE for 

20.2.1.1

.

27.3.1.1  GeneratorFunction ( 

p1

p2

, … , 

pn

body

 )

27.3.2  Properties of the GeneratorFunction Constructor

27.3.2.1  GeneratorFunction.length

27.3.2.2  GeneratorFunction.prototype

27.3.3  Properties of the GeneratorFunction Prototype Object

777

The initial value of 

GeneratorFunction.prototype.constructor

GeneratorFunction.prototype.constructor

 is 

%GeneratorFunction%

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

The initial value of 

GeneratorFunction.prototype.prototype

GeneratorFunction.prototype.prototype

 is the 

Generator prototype object

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

The initial value of the 

@@toStringTag

 property is the String value 

"GeneratorFunction"

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

Every GeneratorFunction instance is an ECMAScript 

function object

 and has the internal slots listed in 

Table 29

. The

value of the [[IsClassConstructor]] internal slot for all such instances is 

false

.

Each GeneratorFunction instance has the following own properties:

The specification for the 

"length"

 property of Function instances given in 

20.2.4.1

 also applies to GeneratorFunction

instances.

The specification for the 

"name"

 property of Function instances given in 

20.2.4.2

 also applies to GeneratorFunction

instances.

Whenever a GeneratorFunction instance is created another 

ordinary object

 is also created and is the initial value of the

generator function's 

"prototype"

 property. The value of the prototype property is used to initialize the [[Prototype]]

internal slot of a newly created Generator object when the generator 

function object

 is invoked using [[Call]].

This property has the attributes { [[Writable]]: 

true

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

NOTE

AsyncGeneratorFunction objects are functions that are usually created by evaluating 

AsyncGeneratorDeclaration

Unlike Function instances, the object that is the value of the a GeneratorFunction's 

"prototype"

property does not have a 

"constructor"

 property whose value is the GeneratorFunction instance.

27.3.3.1  GeneratorFunction.prototype.constructor

27.3.3.2  GeneratorFunction.prototype.prototype

27.3.3.3  GeneratorFunction.prototype [ @@toStringTag ]

27.3.4  GeneratorFunction Instances

27.3.4.1  length

27.3.4.2  name

27.3.4.3  prototype

27.4  AsyncGeneratorFunction Objects

778

AsyncGeneratorExpression

, and 

AsyncGeneratorMethod

 syntactic productions. They may also be created by calling the

%AsyncGeneratorFunction%

 intrinsic.

The AsyncGeneratorFunction 

constructor

:

is 

%AsyncGeneratorFunction%

.

is a subclass of 

Function

Function

.

creates and initializes a new AsyncGeneratorFunction object when called as a function rather than as a

constructor

. Thus the function call 

AsyncGeneratorFunction (...)

AsyncGeneratorFunction (...)

 is equivalent to the object

creation expression 

new AsyncGeneratorFunction (...)

new AsyncGeneratorFunction (...)

 with the same arguments.

is designed to be subclassable. It may be used as the value of an 

extends

extends

 clause of a class definition. Subclass

constructors that intend to inherit the specified AsyncGeneratorFunction behaviour must include a 

super

super

 call

to the AsyncGeneratorFunction 

constructor

 to create and initialize subclass instances with the internal slots

necessary for built-in AsyncGeneratorFunction behaviour. All ECMAScript syntactic forms for defining async
generator function objects create direct instances of AsyncGeneratorFunction. There is no syntactic means to
create instances of AsyncGeneratorFunction subclasses.

The last argument specifies the body (executable code) of an async generator function; any preceding arguments
specify formal parameters.

When the 

AsyncGeneratorFunction

AsyncGeneratorFunction

 function is called with some arguments 

p1

p2

, … , 

pn

body

 (where 

n

might be 0, that is, there are no "

p

" arguments, and where 

body

 might also not be provided), the following steps are

taken:

1.  Let 

C

 be the 

active function object

.

2.  Let 

args

 be the 

argumentsList

 that was passed to this function by [[Call]] or [[Construct]].

3.  Return ? 

CreateDynamicFunction

(

C

, NewTarget, 

asyncGenerator

args

).

NOTE

The AsyncGeneratorFunction 

constructor

:

is a standard built-in 

function object

 that inherits from the Function 

constructor

.

has a [[Prototype]] internal slot whose value is 

%Function%

.

has a 

"name"

 property whose value is 

"AsyncGeneratorFunction"

.

has the following properties:

This is a 

data property

 with a value of 1. This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

,

[[Configurable]]: 

true

 }.

See NOTE for 

20.2.1.1

.

27.4.1  The AsyncGeneratorFunction Constructor

27.4.1.1  AsyncGeneratorFunction ( 

p1

p2

, … , 

pn

body

 )

27.4.2  Properties of the AsyncGeneratorFunction Constructor

27.4.2.1  AsyncGeneratorFunction.length

779

The initial value of 

AsyncGeneratorFunction.prototype

AsyncGeneratorFunction.prototype

 is the 

AsyncGeneratorFunction prototype object

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

The 

AsyncGeneratorFunction prototype object

:

is 

%AsyncGeneratorFunction.prototype%

.

is an 

ordinary object

.

is not a 

function object

 and does not have an [[ECMAScriptCode]] internal slot or any other of the internal slots

listed in 

Table 29

 or 

Table 74

.

has a [[Prototype]] internal slot whose value is 

%Function.prototype%

.

The initial value of 

AsyncGeneratorFunction.prototype.constructor

AsyncGeneratorFunction.prototype.constructor

 is

%AsyncGeneratorFunction%

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

The initial value of 

AsyncGeneratorFunction.prototype.prototype

AsyncGeneratorFunction.prototype.prototype

 is the 

AsyncGenerator prototype

object

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

The initial value of the 

@@toStringTag

 property is the String value 

"AsyncGeneratorFunction"

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

Every AsyncGeneratorFunction instance is an ECMAScript 

function object

 and has the internal slots listed in 

Table 29

.

The value of the [[IsClassConstructor]] internal slot for all such instances is 

false

.

Each AsyncGeneratorFunction instance has the following own properties:

The value of the 

"length"

 property is an 

integral Number

 that indicates the typical number of arguments expected by

the AsyncGeneratorFunction. However, the language permits the function to be invoked with some other number of
arguments. The behaviour of an AsyncGeneratorFunction when invoked on a number of arguments other than the
number specified by its 

"length"

 property depends on the function.

27.4.2.2  AsyncGeneratorFunction.prototype

27.4.3  Properties of the AsyncGeneratorFunction Prototype Object

27.4.3.1  AsyncGeneratorFunction.prototype.constructor

27.4.3.2  AsyncGeneratorFunction.prototype.prototype

27.4.3.3  AsyncGeneratorFunction.prototype [ @@toStringTag ]

27.4.4  AsyncGeneratorFunction Instances

27.4.4.1  length

780

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

The specification for the 

"name"

 property of Function instances given in 

20.2.4.2

 also applies to

AsyncGeneratorFunction instances.

Whenever an AsyncGeneratorFunction instance is created another 

ordinary object

 is also created and is the initial

value of the async generator function's 

"prototype"

 property. The value of the prototype property is used to initialize

the [[Prototype]] internal slot of a newly created AsyncGenerator object when the generator 

function object

 is invoked

using [[Call]].

This property has the attributes { [[Writable]]: 

true

, [[Enumerable]]: 

false

, [[Configurable]]: 

false

 }.

NOTE

A Generator object is an instance of a generator function and conforms to both the 

Iterator

 and 

Iterable

 interfaces.

Generator instances directly inherit properties from the object that is the initial value of the 

"prototype"

 property of

the Generator function that created the instance. Generator instances indirectly inherit properties from the Generator
Prototype intrinsic, 

%GeneratorFunction.prototype.prototype%

.

The 

Generator prototype object

:

is 

%GeneratorFunction.prototype.prototype%

.

is an 

ordinary object

.

is not a Generator instance and does not have a [[GeneratorState]] internal slot.
has a [[Prototype]] internal slot whose value is 

%IteratorPrototype%

.

has properties that are indirectly inherited by all Generator instances.

The initial value of 

Generator.prototype.constructor

Generator.prototype.constructor

 is 

%GeneratorFunction.prototype%

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

The 

next

next

 method performs the following steps:

Unlike function instances, the object that is the value of the an AsyncGeneratorFunction's

"prototype"

 property does not have a 

"constructor"

 property whose value is the

AsyncGeneratorFunction instance.

27.4.4.2  name

27.4.4.3  prototype

27.5  Generator Objects

27.5.1  Properties of the Generator Prototype Object

27.5.1.1  Generator.prototype.constructor

27.5.1.2  Generator.prototype.next ( 

value

 )

781

1.  Let 

g

 be the 

this

 value.

2.  Return ? 

GeneratorResume

(

g

value

empty

).

The 

return

return

 method performs the following steps:

1.  Let 

g

 be the 

this

 value.

2.  Let 

C

 be 

Completion

 { [[Type]]: 

return

, [[Value]]: 

value

, [[Target]]: 

empty

 }.

3.  Return ? 

GeneratorResumeAbrupt

(

g

C

empty

).

The 

throw

throw

 method performs the following steps:

1.  Let 

g

 be the 

this

 value.

2.  Let 

C

 be 

ThrowCompletion

(

exception

).

3.  Return ? 

GeneratorResumeAbrupt

(

g

C

empty

).

The initial value of the 

@@toStringTag

 property is the String value 

"Generator"

.

This property has the attributes { [[Writable]]: 

false

, [[Enumerable]]: 

false

, [[Configurable]]: 

true

 }.

Generator instances are initially created with the internal slots described in 

Table 73

.

Table 73: Internal Slots of Generator Instances

Internal Slot

Description

[[GeneratorState]]

The current execution state of the generator. The possible values are: 

undefined

,

suspendedStart

suspendedYield

executing

, and 

completed

.

[[GeneratorContext]] The 

execution context

 that is used when executing the code of this generator.

[[GeneratorBrand]]

A brand used to distinguish different kinds of generators. The [[GeneratorBrand]] of
generators declared by ECMAScript source text is always 

empty

.

The abstract operation GeneratorStart takes arguments 

generator

 and 

generatorBody

 (a 

Parse Node

 or an 

Abstract

Closure

 with no parameters). It performs the following steps when called:

1. 

Assert

: The value of 

generator

.[[GeneratorState]] is 

undefined

.

2.  Let 

genContext

 be the 

running execution context

.

27.5.1.3  Generator.prototype.return ( 

value

 )

27.5.1.4  Generator.prototype.throw ( 

exception

 )

27.5.1.5  Generator.prototype [ @@toStringTag ]

27.5.2  Properties of Generator Instances

27.5.3  Generator Abstract Operations

27.5.3.1  GeneratorStart ( 

generator

generatorBody

 )

782

3.  Set the Generator component of 

genContext

 to 

generator

.

4.  Set the code evaluation state of 

genContext

 such that when evaluation is resumed for that 

execution context

 the

following steps will be performed:

a.  If 

generatorBody

 is a 

Parse Node

, then

i.  Let 

result

 be the result of evaluating 

generatorBody

.

b.  Else,

i. 

Assert

generatorBody

 is an 

Abstract Closure

 with no parameters.

ii.  Let 

result

 be 

generatorBody

().

c. 

Assert

: If we return here, the generator either threw an exception or performed either an implicit or

explicit return.

d.  Remove 

genContext

 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

.

e.  Set 

generator

.[[GeneratorState]] to 

completed

.

f.  Once a generator enters the 

completed

 state it never leaves it and its associated 

execution context

 is

never resumed. Any execution state associated with 

generator

 can be discarded at this point.

g.  If 

result

.[[Type]] is 

normal

, let 

resultValue

 be 

undefined

.

h.  Else if 

result

.[[Type]] is 

return

, let 

resultValue

 be 

result

.[[Value]].

i.  Else,

i. 

Assert

result

.[[Type]] is 

throw

.

ii.  Return 

Completion

(

result

).

j.  Return 

CreateIterResultObject

(

resultValue

true

).

5.  Set 

generator

.[[GeneratorContext]] to 

genContext

.

6.  Set 

generator

.[[GeneratorState]] to 

suspendedStart

.

7.  Return 

NormalCompletion

(

undefined

).

The abstract operation GeneratorValidate takes arguments 

generator

 and 

generatorBrand

. It performs the following

steps when called:

1.  Perform ? 

RequireInternalSlot

(

generator

, [[GeneratorState]]).

2.  Perform ? 

RequireInternalSlot

(

generator

, [[GeneratorBrand]]).

3.  If 

generator

.[[GeneratorBrand]] is not the same value as 

generatorBrand

, throw a 

TypeError

 exception.

4. 

Assert

generator

 also has a [[GeneratorContext]] internal slot.

5.  Let 

state

 be 

generator

.[[GeneratorState]].

6.  If 

state

 is 

executing

, throw a 

TypeError

 exception.

7.  Return 

state

.

The abstract operation GeneratorResume takes arguments 

generator

value

, and 

generatorBrand

. It performs the

following steps when called:

1.  Let 

state

 be ? 

GeneratorValidate

(

generator

generatorBrand

).

2.  If 

state

 is 

completed

, return 

CreateIterResultObject

(

undefined

true

).

3. 

Assert

state

 is either 

suspendedStart

 or 

suspendedYield

.

4.  Let 

genContext

 be 

generator

.[[GeneratorContext]].

5.  Let 

methodContext

 be the 

running execution context

.

27.5.3.2  GeneratorValidate ( 

generator

generatorBrand

 )

27.5.3.3  GeneratorResume ( 

generator

value

generatorBrand

 )

783

 

 

 

 

 

 

 

Content      ..     56      57      58      59     ..