async-await-optimization.js 2.6 KB
Newer Older
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 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
// Copyright 2018 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --harmony-await-optimization

// test basic interleaving
(function () {
  const actual = [];
  const expected = [ 'await', 1, 'await', 2 ];
  const iterations = 2;

  async function pushAwait() {
    actual.push('await');
  }

  async function callAsync() {
    for (let i = 0; i < iterations; i++) {
      await pushAwait();
    }
    return 0;
  }

  function checkAssertions() {
    assertArrayEquals(expected, actual,
      'Async/await and promises should be interleaved.');
  }

  assertPromiseResult((async() => {
    callAsync();

    return new Promise(function (resolve) {
      actual.push(1);
      resolve();
    }).then(function () {
      actual.push(2);
    }).then(checkAssertions);
  })());
})();

// test async generators
(function () {
  const actual = [];
  const expected = [ 'await', 1, 'await', 2 ];
  const iterations = 2;

  async function pushAwait() {
    actual.push('await');
  }

  async function* callAsync() {
    for (let i = 0; i < iterations; i++) {
      await pushAwait();
    }
    return 0;
  }

  function checkAssertions() {
    assertArrayEquals(expected, actual,
      'Async/await and promises should be interleaved when using async generators.');
  }

  assertPromiseResult((async() => {
    callAsync().next();

    return new Promise(function (resolve) {
      actual.push(1);
      resolve();
    }).then(function () {
      actual.push(2);
    }).then(checkAssertions);
  })());
})();

// test yielding from async generators
(function () {
  const actual = [];
  const expected = [
    'Promise: 6',
    'Promise: 5',
    'Await: 3',
    'Promise: 4',
    'Promise: 3',
    'Await: 2',
    'Promise: 2',
    'Promise: 1',
    'Await: 1',
    'Promise: 0'
  ];
  const iterations = 3;

  async function* naturalNumbers(start) {
    let current = start;
    while (current > 0) {
      yield Promise.resolve(current--);
    }
  }

  async function trigger() {
    for await (const num of naturalNumbers(iterations)) {
      actual.push('Await: ' + num);
    }
  }

  async function checkAssertions() {
    assertArrayEquals(expected, actual,
      'Async/await and promises should be interleaved when yielding.');
  }

  async function countdown(counter) {
    actual.push('Promise: ' + counter);
    if (counter > 0) {
      return Promise.resolve(counter - 1).then(countdown);
    } else {
      await checkAssertions();
    }
  }

  assertPromiseResult((async() => {
    trigger();

    return countdown(iterations * 2);
  })());
})();