| 1 |
// Promise.race(...) may add a dummy PromiseReaction which is only used for the |
| 2 |
// debugger. Ensure that this dummy reaction can't influence the normal Promise |
| 3 |
// resolution behaviour. |
| 4 |
// |
| 5 |
// See BlockOnPromise when called from PerformPromiseRace for when this dummy |
| 6 |
// reaction is created. |
| 7 |
|
| 8 |
function newPromiseCapability() { |
| 9 |
var resolve, reject, promise = new Promise(function(r1, r2) { |
| 10 |
resolve = r1; |
| 11 |
reject = r2; |
| 12 |
}); |
| 13 |
return {promise, resolve, reject}; |
| 14 |
} |
| 15 |
|
| 16 |
function neverCalled() { |
| 17 |
// Quit with non-zero exit code to ensure a test suite error is shown, |
| 18 |
// even when this function is called within promise handlers which normally |
| 19 |
// swallow any exceptions. |
| 20 |
quit(1); |
| 21 |
} |
| 22 |
|
| 23 |
var c = 0; |
| 24 |
var g_resolve; |
| 25 |
|
| 26 |
var resolvedValues = []; |
| 27 |
|
| 28 |
function resolveCapability(v) { |
| 29 |
resolvedValues.push(v); |
| 30 |
} |
| 31 |
|
| 32 |
class P extends Promise { |
| 33 |
constructor(executor) { |
| 34 |
// Only the very first object created through this constructor gets |
| 35 |
// special treatment, all other invocations create built-in Promise |
| 36 |
// objects. |
| 37 |
if (c++ > 1) { |
| 38 |
return new Promise(executor); |
| 39 |
} |
| 40 |
|
| 41 |
executor(resolveCapability, neverCalled); |
| 42 |
|
| 43 |
var {promise, resolve} = newPromiseCapability(); |
| 44 |
g_resolve = resolve; |
| 45 |
|
| 46 |
// Use an async function to create a Promise without resolving functions. |
| 47 |
var p = async function(){ await promise; return 456; }(); |
| 48 |
|
| 49 |
// Ensure the species constructor is not the built-in Promise constructor |
| 50 |
// to avoid falling into the fast path. |
| 51 |
p.constructor = { |
| 52 |
[Symbol.species]: P |
| 53 |
}; |
| 54 |
|
| 55 |
return p; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
var {promise: alwaysPending} = newPromiseCapability(); |
| 60 |
|
| 61 |
// The promise returned from race() should never be resolved. |
| 62 |
P.race([alwaysPending]).then(neverCalled, neverCalled); |
| 63 |
|
| 64 |
g_resolve(123); |
| 65 |
|
| 66 |
drainJobQueue(); |
| 67 |
|
| 68 |
// Check |resolvedValues| to ensure resolving functions were properly called. |
| 69 |
assertEq(resolvedValues.length, 2); |
| 70 |
assertEq(resolvedValues[0], alwaysPending); |
| 71 |
assertEq(resolvedValues[1], 456); |