summaryrefslogtreecommitdiff
path: root/test/built-ins/Promise/prototype/then/capability-executor-called-twice.js
blob: f72869ba3fbc04f34589f0c47290e28f19e5f8a1 (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
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
// Copyright (C) 2015 André Bargull. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
es6id: 25.4.5.3
description: >
  Throws a TypeError if capabilities executor already called with non-undefined values.
info: |
  Promise.prototype.then ( onFulfilled , onRejected )

  ...
  5. Let promiseCapability be NewPromiseCapability(C).
  6. ReturnIfAbrupt(promiseCapability).
  ...

  25.4.1.5.1 GetCapabilitiesExecutor Functions
    ...
    3. If promiseCapability.[[Resolve]] is not undefined, throw a TypeError exception.
    4. If promiseCapability.[[Reject]] is not undefined, throw a TypeError exception.
    5. Set promiseCapability.[[Resolve]] to resolve.
    6. Set promiseCapability.[[Reject]] to reject.
    ...
features: [class]
---*/

var constructorFunction;

var promise = new class extends Promise {
  constructor(executor) {
    if (constructorFunction) {
      constructorFunction(executor);
      return {};
    }
    return super(executor);
  }
}(function() {});

var checkPoint = "";
constructorFunction = function(executor) {
  checkPoint += "a";
  executor();
  checkPoint += "b";
  executor(function() {}, function() {});
  checkPoint += "c";
};
promise.then();
assert.sameValue(checkPoint, "abc", "executor initially called with no arguments");

var checkPoint = "";
constructorFunction = function(executor) {
  checkPoint += "a";
  executor(undefined, undefined);
  checkPoint += "b";
  executor(function() {}, function() {});
  checkPoint += "c";
};
promise.then();
assert.sameValue(checkPoint, "abc", "executor initially called with (undefined, undefined)");

var checkPoint = "";
assert.throws(TypeError, function() {
  constructorFunction = function(executor) {
    checkPoint += "a";
    executor(undefined, function() {});
    checkPoint += "b";
    executor(function() {}, function() {});
    checkPoint += "c";
  };
  promise.then();
}, "executor initially called with (undefined, function)");
assert.sameValue(checkPoint, "ab", "executor initially called with (undefined, function)");

var checkPoint = "";
assert.throws(TypeError, function() {
  constructorFunction = function(executor) {
    checkPoint += "a";
    executor(function() {}, undefined);
    checkPoint += "b";
    executor(function() {}, function() {});
    checkPoint += "c";
  };
  promise.then();
}, "executor initially called with (function, undefined)");
assert.sameValue(checkPoint, "ab", "executor initially called with (function, undefined)");

var checkPoint = "";
assert.throws(TypeError, function() {
  constructorFunction = function(executor) {
    checkPoint += "a";
    executor("invalid value", 123);
    checkPoint += "b";
    executor(function() {}, function() {});
    checkPoint += "c";
  };
  promise.then();
}, "executor initially called with (String, Number)");
assert.sameValue(checkPoint, "ab", "executor initially called with (String, Number)");