tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

promise-any-with-non-default-resolving.js (1519B)


      1 // |jit-test| skip-if: !Promise.any
      2 
      3 function newPromiseCapability() {
      4    var resolve, reject, promise = new Promise(function(r1, r2) {
      5        resolve = r1;
      6        reject = r2;
      7    });
      8    return {promise, resolve, reject};
      9 }
     10 
     11 function neverCalled() {
     12    // Quit with non-zero exit code to ensure a test suite error is shown,
     13    // even when this function is called within promise handlers which normally
     14    // swallow any exceptions.
     15    quit(1);
     16 }
     17 
     18 var {promise, resolve} = newPromiseCapability();
     19 
     20 var getterCount = 0;
     21 
     22 class P extends Promise {
     23    constructor(executor) {
     24        var {promise, resolve, reject} = newPromiseCapability();
     25 
     26        executor(function(v) {
     27            // Resolve the promise.
     28            resolve(v);
     29 
     30            // But then return an object from the resolve function. This object
     31            // must be treated as the resolution value for the otherwise
     32            // skipped promise which gets created when Promise.prototype.then is
     33            // called in PerformPromiseRace.
     34            return {
     35                get then() {
     36                    getterCount++;
     37                }
     38            };
     39        }, neverCalled);
     40 
     41        return promise;
     42    }
     43 
     44    // Default to the standard Promise.resolve function, so we don't create
     45    // another instance of this class when resolving the passed promise objects
     46    // in Promise.race.
     47    static resolve(v) {
     48        return Promise.resolve(v);
     49    }
     50 }
     51 
     52 P.any([promise]);
     53 
     54 resolve(0);
     55 
     56 drainJobQueue();
     57 
     58 assertEq(getterCount, 1);