tor-browser

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

browser_test_has_promise_resolved.js (1739B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 add_task(async function test_promiseResolved() {
      7  const { promise, resolve } = Promise.withResolvers();
      8 
      9  ok(!(await hasPromiseResolved(promise)));
     10  resolve();
     11  ok(await hasPromiseResolved(promise));
     12 });
     13 
     14 add_task(async function test_promiseRejected() {
     15  const { promise, reject } = Promise.withResolvers();
     16 
     17  ok(!(await hasPromiseResolved(promise)));
     18  reject(new Error("Some message"));
     19  ok(await hasPromiseResolved(promise));
     20 
     21  await Assert.rejects(
     22    promise,
     23    e => e.message == "Some message",
     24    "Caught the expected error"
     25  );
     26 });
     27 
     28 add_task(async function test_asyncResolved() {
     29  const { promise, resolve } = Promise.withResolvers();
     30  async function simpleAsyncMethod() {
     31    // Drive this method from the external Promise.
     32    await promise;
     33  }
     34 
     35  const onSimpleAsyncMethod = simpleAsyncMethod();
     36  ok(!(await hasPromiseResolved(onSimpleAsyncMethod)));
     37  resolve();
     38  ok(await hasPromiseResolved(onSimpleAsyncMethod));
     39 });
     40 
     41 add_task(async function test_asyncRejected() {
     42  const { promise, resolve } = Promise.withResolvers();
     43 
     44  // This type of method used to be badly handled by hasPromiseResolved.
     45  // See Bug 1927144.
     46  async function asyncMethodErroring() {
     47    // Drive this method from the external Promise.
     48    await promise;
     49    throw new Error("Some message");
     50  }
     51 
     52  const onAsyncMethodErroring = asyncMethodErroring();
     53  ok(!(await hasPromiseResolved(onAsyncMethodErroring)));
     54  resolve();
     55  ok(await hasPromiseResolved(onAsyncMethodErroring));
     56 
     57  await Assert.rejects(
     58    onAsyncMethodErroring,
     59    e => e.message == "Some message",
     60    "Caught the expected error"
     61  );
     62 });