summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/AsyncIterator/prototype/lazy-methods-iterator-not-closed-on-next-then-throws.js
blob: 609a49c3c75102fc70a73d1e9f1e26a3692b7093 (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
// |reftest| skip-if(!this.hasOwnProperty('AsyncIterator'))

//
//
/*---
esid: pending
description: Lazy %AsyncIterator.prototype% methods don't close the iterator if `then` returned by `next` throws.
info: >
    AsyncIterator Helpers proposal 2.1.6
features: [iterator-helpers]
---*/

class TestError extends Error {}

class TestIterator extends AsyncIterator {
  next() {
    return {
      then() {
        throw new TestError();
      }
    };
  }

  closed = false;
  async return(value) {
    this.closed = true;
    return {done: true, value};
  }
}

const methods = [
  ["map", x => x],
  ["filter", x => true],
  ["take", Infinity],
  ["drop", 0],
  ["asIndexedPairs", undefined],
  ["flatMap", async function*(x) { yield x; }],
];

(async () => {
  for (const [method, arg] of methods) {
    const iterator = new TestIterator();
    assertEq(iterator.closed, false);

    try {
      await iterator[method](arg).next();
      assertEq(true, false, 'Expected exception');
    } catch(err) {
      console.log(err);
      assertEq(err instanceof TestError, true);
    }
    assertEq(iterator.closed, false);
  }
})();

if (typeof reportCompare == 'function')
  reportCompare(0, 0);