chain(onSuccess, onError) {
    const newPromise = new shaka.util.PublicPromise();
    // Silence uncaught rejection errors, which may otherwise occur any place
    // we don't explicitly handle aborted operations.
    newPromise.catch(() => {});
    const abortError = shaka.util.AbortableOperation.abortError();

    // If called before "this" completes, just abort "this".
    let abort = () => {
      newPromise.reject(abortError);
      return this.abort();
    };

    const makeCallback = (isSuccess) => {
      return (value) => {
        if (this.abortPromise_ && isSuccess) {
          // If "this" is not abortable(), or if abort() is called after "this"
          // is complete but before the next stage in the chain begins, we
          // should stop right away.
          newPromise.reject(abortError);
          return;
        }

        const cb = isSuccess ? onSuccess : onError;
        if (!cb) {
          // No callback?  Pass it along.
          const next = isSuccess ? newPromise.resolve : newPromise.reject;
          next(value);
          return;
        }

        // Call the callback, interpret the return value, set the Promise state,
        // and get the next abort function.
        abort = shaka.util.AbortableOperation.wrapChainCallback_(
            cb, value, newPromise);
      };
    };
    this.promise.then(makeCallback(true), makeCallback(false));

    return new shaka.util.AbortableOperation(
        newPromise,
        // By creating a closure around abort(), we can update the value of
        // abort() at various stages.
        () => abort());
  }
@param {(undefined|
         function(T):U|
         function(T):!Promise<U>|
         function(T):!shaka.util.AbortableOperation.<U>)} onSuccess
  A callback to be invoked after this operation is complete, to chain to
  another operation.  The callback can return a plain value, a Promise to
  an asynchronous value, or another AbortableOperation.
@param {function(*)=} onError
  An optional callback to be invoked if this operation fails, to perform
  some cleanup or error handling.  Analogous to the second parameter of
  Promise.prototype.then.
@return {!shaka.util.AbortableOperation.<U>} An operation which is resolved
  when this operation and the operation started by the callback are both
  complete.
@template U
@export