Debian Codegraph @

js/src/tests/test262/language/expressions/await/await-monkey-patched-promise.js

raw
1 // |reftest| async
2 // Copyright 2018 the V8 project authors. All rights reserved.
3 // This code is governed by the BSD license found in the LICENSE file.
4
5 /*---
6 author: Maya Lekova <[email protected]>
7 esid: await
8 description: >
9 This test demonstrates that monkey-patched "then" on native promises will
10 not get called. Adapted from example by Kevin Smith:
11 https://github.com/tc39/ecma262/pull/1250#issuecomment-401082195
12 flags: [async]
13 features: [async-functions]
14 includes: [compareArray.js]
15 ---*/
16
17 let thenCallCount = 0;
18 const value = 42;
19
20 const actual = [];
21 const expected = [
22 'Promise: 1',
23 'Await: ' + value,
24 'Promise: 2',
25 ];
26
27 const patched = Promise.resolve(value);
28 patched.then = function(...args) {
29 thenCallCount++;
30 Promise.prototype.then.apply(this, args);
31 };
32
33 async function trigger() {
34 actual.push('Await: ' + await patched);
35 }
36
37 function checkAssertions() {
38 assert.compareArray(actual, expected,
39 'Async/await and promises should be interleaved');
40 assert.sameValue(thenCallCount, 0,
41 'Monkey-patched "then" on native promises should not be called.')
42 }
43
44 trigger().then(checkAssertions).then($DONE, $DONE);
45
46 new Promise(function (resolve) {
47 actual.push('Promise: 1');
48 resolve();
49 }).then(function () {
50 actual.push('Promise: 2');
51 });