function maybeHandlePromiseRejection(
  value,
  onError,
  onFinally,
) {
  if (utils.isThenable(value)) {
    // @ts-expect-error - the isThenable check returns the "wrong" type here
    return value.then(
      res => {
        onFinally();
        return res;
      },
      e => {
        onError(e);
        onFinally();
        throw e;
      },
    );
  }

  onFinally();
  return value;
}
Maybe handle a promise rejection.
This expects to be given a value that _may_ be a promise, or any other value.
If it is a promise, and it rejects, it will call the `onError` callback.
Other than this, it will generally return the given value as-is.