summaryrefslogtreecommitdiffstats
path: root/js/src/builtin/Promise.js
blob: 91a1e1f5620bc19173ec50b6d0ca905f127d0261 (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// ES6, 25.4.4.6.
function Promise_static_get_species() {
    // Step 1.
    return this;
}
_SetCanonicalName(Promise_static_get_species, "get [Symbol.species]");

// ES6, 25.4.5.1.
function Promise_catch(onRejected) {
    // Steps 1-2.
    return callContentFunction(this.then, this, undefined, onRejected);
}

// Promise.prototype.finally(onFinally)
// See https://tc39.es/proposal-promise-finally/
function Promise_finally(onFinally) {
    // Step 1.
    var promise = this;

    // Step 2.
    if (!IsObject(promise))
        ThrowTypeError(JSMSG_INCOMPATIBLE_PROTO, "Promise", "finally", "value");

    // Step 3.
    var C = SpeciesConstructor(promise, GetBuiltinConstructor("Promise"));

    // Step 4.
    assert(IsConstructor(C), "SpeciesConstructor returns a constructor function");

    // Steps 5-6.
    var thenFinally, catchFinally;
    if (!IsCallable(onFinally)) {
        thenFinally = onFinally;
        catchFinally = onFinally;
    } else {
        // ThenFinally Function.
        // The parentheses prevent the inferring of a function name.
        (thenFinally) = function(value) {
            // Steps 1-2 (implicit).

            // Step 3.
            var result = onFinally();

            // Steps 4-5 (implicit).

            // Step 6.
            var promise = PromiseResolve(C, result);

            // Step 7.
            // FIXME: spec issue - "be equivalent to a function that" is not a defined spec term.
            // https://github.com/tc39/ecma262/issues/933

            // Step 8.
            return callContentFunction(promise.then, promise, function() { return value; });
        };

        // CatchFinally Function.
        // The parentheses prevent the inferring of a function name.
        (catchFinally) = function(reason) {
            // Steps 1-2 (implicit).

            // Step 3.
            var result = onFinally();

            // Steps 4-5 (implicit).

            // Step 6.
            var promise = PromiseResolve(C, result);

            // Step 7.
            // FIXME: spec issue - "be equivalent to a function that" is not a defined spec term.
            // https://github.com/tc39/ecma262/issues/933

            // Step 8.
            return callContentFunction(promise.then, promise, function() { throw reason; });
        };
    }

    // Step 7.
    return callContentFunction(promise.then, promise, thenFinally, catchFinally);
}