tor-browser

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

invoke-resolve-get-once-multiple-calls.js (1568B)


      1 // |reftest| async
      2 // Copyright (C) 2019 Leo Balter, 2020 Rick Waldron. All rights reserved.
      3 // This code is governed by the BSD license found in the LICENSE file.
      4 
      5 /*---
      6 description: >
      7  Gets constructor's `resolve` method once from zero to many invocations.
      8 esid: sec-promise.any
      9 info: |
     10  5. Let result be PerformPromiseAny(iteratorRecord, C, promiseCapability).
     11  6. If result is an abrupt completion, then
     12    a. If iteratorRecord.[[Done]] is false, set result to IteratorClose(iteratorRecord, result).
     13    b. IfAbruptRejectPromise(result, promiseCapability).
     14 
     15  Runtime Semantics: PerformPromiseAny
     16 
     17  6. Let promiseResolve be ? Get(constructor, "resolve").
     18  7. If ! IsCallable(promiseResolve) is false, throw a TypeError exception.
     19  8. Repeat
     20    ...
     21    i. Let nextPromise be ? Call(promiseResolve, constructor, « nextValue »).
     22 
     23 flags: [async]
     24 features: [Promise.any, arrow-function, destructuring-binding]
     25 ---*/
     26 
     27 let promises = [
     28  Promise.reject(1),
     29  Promise.reject(1),
     30  Promise.reject(1),
     31 ];
     32 let boundPromiseResolve = Promise.resolve.bind(Promise);
     33 let getCount = 0;
     34 let callCount = 0;
     35 
     36 Object.defineProperty(Promise, 'resolve', {
     37  configurable: true,
     38  get() {
     39    getCount += 1;
     40    return function(...args) {
     41      callCount += 1;
     42      return boundPromiseResolve(...args);
     43    };
     44  }
     45 });
     46 
     47 Promise.any(promises).then(() => {
     48    $DONE('The promise should be rejected, but was resolved');
     49  }, ({errors}) => {
     50    assert.sameValue(getCount, 1);
     51    assert.sameValue(callCount, 3);
     52    assert.sameValue(errors.length, 3);
     53  }).then($DONE, $DONE);