function baseHandledPromise(executor, pendingHandler = undefined) {
    new.target || Fail`must be invoked with "new"`;
    let handledResolve;
    let handledReject;
    let resolved = false;
    let resolvedTarget = null;
    /** @type {Promise<R>} */
    let handledP;
    let continueForwarding = () => {};
    const assertNotYetForwarded = () => {
      !forwardedPromiseToPromise.has(handledP) ||
        assert.fail(X`internal: already forwarded`, TypeError);
    };
    const superExecutor = (superResolve, superReject) => {
      handledResolve = value => {
        if (resolved) {
          return;
        }
        assertNotYetForwarded();
        value = shorten(value);
        let targetP;
        if (
          promiseToPendingHandler.has(value) ||
          promiseToPresence.has(value)
        ) {
          targetP = value;
        } else {
          // We're resolving to a non-promise, so remove our handler.
          promiseToPendingHandler.delete(handledP);
          targetP = presenceToPromise.get(value);
        }
        // Ensure our data structure is a proper tree (avoid cycles).
        if (targetP && !objectIs(targetP, handledP)) {
          forwardedPromiseToPromise.set(handledP, targetP);
        } else {
          forwardedPromiseToPromise.delete(handledP);
        }

        // Remove stale pending handlers, set to canonical form.
        shorten(handledP);

        // Finish the resolution.
        superResolve(value);
        resolved = true;
        resolvedTarget = value;

        // We're resolved, so forward any postponed operations to us.
        continueForwarding();
      };
      handledReject = reason => {
        if (resolved) {
          return;
        }
        harden(reason);
        assertNotYetForwarded();
        promiseToPendingHandler.delete(handledP);
        resolved = true;
        superReject(reason);
        continueForwarding();
      };
    };
    handledP = harden(construct(Promise, [superExecutor], new.target));

    if (!pendingHandler) {
      // This is insufficient for actual remote handled Promises
      // (too many round-trips), but is an easy way to create a
      // local handled Promise.
      [pendingHandler, continueForwarding] =
        makePostponedHandler(HandledPromise);
    }

    const validateHandler = h => {
      Object(h) === h ||
        assert.fail(X`Handler ${h} cannot be a primitive`, TypeError);
    };
    validateHandler(pendingHandler);

    // Until the handled promise is resolved, we use the pendingHandler.
    promiseToPendingHandler.set(handledP, pendingHandler);

    const rejectHandled = reason => {
      if (resolved) {
        return;
      }
      assertNotYetForwarded();
      handledReject(reason);
    };

    const resolveWithPresence = (
      presenceHandler = pendingHandler,
      options = {},
    ) => {
      if (resolved) {
        return resolvedTarget;
      }
      assertNotYetForwarded();
      try {
        // Sanity checks.
        validateHandler(presenceHandler);

        const { proxy: proxyOpts } = options;
        let presence;
        if (proxyOpts) {
          const {
            handler: proxyHandler,
            // The proxy target can be frozen but should not be hardened
            // so it remains trapping.
            // See https://github.com/endojs/endo/blob/master/packages/ses/docs/preparing-for-stabilize.md
            target: proxyTarget,
            revokerCallback,
          } = proxyOpts;
          if (revokerCallback) {
            // Create a proxy and its revoke function.
            const { proxy, revoke } = Proxy.revocable(
              proxyTarget,
              proxyHandler,
            );
            presence = proxy;
            revokerCallback(revoke);
          } else {
            presence = new Proxy(proxyTarget, proxyHandler);
          }
        } else {
          // Default presence.
          presence = create(null);
        }

        // Validate and install our mapped target (i.e. presence).
        resolvedTarget = presence;

        // Create table entries for the presence mapped to the
        // fulfilledHandler.
        presenceToPromise.set(resolvedTarget, handledP);
        promiseToPresence.set(handledP, resolvedTarget);
        presenceToHandler.set(resolvedTarget, presenceHandler);

        // We committed to this presence, so resolve.
        handledResolve(resolvedTarget);
        return resolvedTarget;
      } catch (e) {
        annotateError(e, X`during resolveWithPresence`);
        handledReject(e);
        throw e;
      }
    };

    const resolveHandled = target => {
      if (resolved) {
        return;
      }
      assertNotYetForwarded();
      try {
        // Resolve the target.
        handledResolve(target);
      } catch (e) {
        handledReject(e);
      }
    };

    // Invoke the callback to let the user resolve/reject.
    executor(resolveHandled, rejectHandled, resolveWithPresence);

    return handledP;
  }
This *needs* to be a `function X` so that we can use it as a constructor.

@template R
@param {HandledExecutor<R>} executor
@param {Handler<Promise<R>>} [pendingHandler]
@returns {Promise<R>}