tor-browser

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

promise-race-with-non-default-resolving.js (1483B)


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