[whatwg/dom] Adding a return value to `controller.abort()` which represents abort completion (#881)

Consider the following code we're adding as an example to the Streams Standard in https://github.com/whatwg/streams/pull/1059:

```js
const controller = new AbortController();
const pipePromise = readable1.pipeTo(writable, { preventAbort: true, signal: controller.signal });

// ... some time later ...
controller.abort();

// Wait for the pipe to complete before starting a new one:
try {
 await pipePromise;
} catch (e) {
 // Swallow "AbortError" DOMExceptions as expected, but rethrow any unexpected failures.
 if (e.name !== "AbortError") {
  throw e;
 }
}

// Start the new pipe!
readable2.pipeTo(writable);
```

Wouldn't it be nice if we could do this instead?

```js
const controller = new AbortController();
const pipePromise = readable1.pipeTo(writable, { preventAbort: true, signal: controller.signal });

// ... some time later ...
await controller.abort();

// Start the new pipe!
readable2.pipeTo(writable);
```

The idea here is that the piping operation communicates back to the AbortSignal when the abort completes, and whether it did so successfully. And this can be surfaced as the return value of `abort()`.

API idea for JavaScript `AbortSignal` consumers:

```js
signal.addEventListener("abort", e => {
  e.respondWith(promise); // must be called synchronously during dispatch of "abort"

  e.respondWith(promise2); // causes a Promise.all() on promise and promise2 to be communicated back.

  // Promise fulfillment values are ignored; controller.abort() always returns Promise<void>.
});
```

(a similar thing would exist for spec consumers of AbortSignal.)

Question: what is the default behavior if there are no calls to `respondWith()`? I think probably we return an immediately-fulfilled promise, indicating that the abort request succeeded and nobody signaled that it would take a long time or that it would fail. Alternately, we could have it be forever-pending, under the logic that you need to call `respondWith()` to properly signal that the abort succeeded... but that doesn't seem very user-friendly.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/881

Received on Thursday, 23 July 2020 21:52:19 UTC