summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/AsyncFromSyncIteratorPrototype/next/yield-next-rejected-promise-close.js
blob: d747bce581fd7cb7a01c73c8b32e836965803b27 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// |reftest| async
// Copyright (C) 2024 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-%asyncfromsynciteratorprototype%.next
description: next() will reject promise if resolving result promise abrupt completes.
info: |
  %AsyncFromSyncIteratorPrototype%.next ( value )
  ...
  3. Let promiseCapability be ! NewPromiseCapability(%Promise%).
  4. Let syncIteratorRecord be O.[[SyncIteratorRecord]].
  5. If value is present, then
    ...
  6. Else,
    a. Let result be IteratorNext(syncIteratorRecord).
  7. IfAbruptRejectPromise(result, promiseCapability).
  8. Return ! AsyncFromSyncIteratorContinuation(result, promiseCapability, syncIteratorRecord, true).

  AsyncFromSyncIteratorContinuation ( result, promiseCapability, syncIteratorRecord, closeOnRejection )
  1. Let done be IteratorComplete(result).
  2. IfAbruptRejectPromise(done, promiseCapability).
  3. Let value be IteratorValue(result).
  4. IfAbruptRejectPromise(value, promiseCapability).
  5. Let valueWrapper be PromiseResolve(%Promise%, value).
  6. If valueWrapper is an abrupt completion, done is false, and closeOnRejection is true, then
    a. Set valueWrapper to IteratorClose(syncIteratorRecord, valueWrapper).
  ...
  12. If done is true, or if closeOnRejection is false, then
    ...
  13. Else,
    a. Let closeIterator be a new Abstract Closure with parameters (error) that captures syncIteratorRecord and performs the following steps when called:
        i. Return ? IteratorClose(syncIteratorRecord, ThrowCompletion(error)).
    b. Let onRejected be CreateBuiltinFunction(closeIterator, 1, "", « »).
    c. NOTE: onRejected is used to close the Iterator when the "value" property of an IteratorResult object it yields is a rejected promise.
  14. Perform PerformPromiseThen(valueWrapper, onFulfilled, onRejected, promiseCapability).
  15. Return promiseCapability.[[Promise]].


flags: [async]
features: [async-iteration]
includes: [asyncHelpers.js]
---*/

var returnCount = 0;
function Reject() {}

const syncIterator = {
  [Symbol.iterator]() {
    return {
      next() {
        return { value: Promise.reject(new Reject()), done: false };
      },
      return() {
        returnCount += 1;
      }
    };
  }
}

async function* asyncIterator() {
  yield* syncIterator;
}

asyncTest(async () => {
  await assert.throwsAsync(Reject, async () => asyncIterator().next(), "Promise should be rejected");
  assert.sameValue(returnCount, 1);
});