summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/promise/promise-race-with-default-resolving-internal.js
blob: 815124c46f3fd300dca6777af31f6dc95e0ddd6c (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
function newPromiseCapability() {
    let resolve, reject, promise = new Promise(function(r1, r2) {
        resolve = r1;
        reject = r2;
    });
    return {promise, resolve, reject};
}

function neverCalled() {
    // Quit with non-zero exit code to ensure a test suite error is shown,
    // even when this function is called within promise handlers which normally
    // swallow any exceptions.
    quit(1);
}

var c = 0;
var g_resolve;

class P extends Promise {
    constructor(executor) {
        // Only the very first object created through this constructor gets
        // special treatment, all other invocations create built-in Promise
        // objects.
        if (c++ > 1) {
            return new Promise(executor);
        }

        // Pass a native ResolvePromiseFunction function as the resolve handler.
        // (It's okay that the promise of this promise capability is never used.)
        executor(newPromiseCapability().resolve, neverCalled);

        let {promise, resolve} = newPromiseCapability();
        g_resolve = resolve;

        // Use an async function to create a Promise without resolving functions.
        return async function(){ await promise; return 456; }();
    }

    // Ensure we don't take the (spec) fast path in Promise.resolve and instead
    // create a new promise object. (We could not provide an override at all
    // and rely on the default behaviour, but giving an explicit definition
    // may help to interpret this test case.)
    static resolve(v) {
        return super.resolve(v);
    }
}

let {promise: alwaysPending} = newPromiseCapability();

P.race([alwaysPending]).then(neverCalled, neverCalled);

g_resolve(123);

drainJobQueue();