Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AbortController - Reaffirming promise reject & AbortError semantics #33540

Closed
Bnaya opened this issue May 24, 2020 · 4 comments
Closed

AbortController - Reaffirming promise reject & AbortError semantics #33540

Bnaya opened this issue May 24, 2020 · 4 comments
Labels
discuss Issues opened for discussions and feedbacks.

Comments

@Bnaya
Copy link

Bnaya commented May 24, 2020

Per #33528

TL;DR;

  • We need that abortable async node api to reject with AbortError.
  • On the browser AbortError is instanceof DOMException,
  • We don't have DOMException in node, so we need to make it instanceof Error
  • abortError = new Error(), abortError.name = "AbortError";
  • We need to document that
  • unfortunately browser code that uses error instanceof DOMException && error.name === "AbortError" will not detect it

On the browser, when async flow is aborted using AbortController, the flow is expected to return rejected promise, with an AbortError ().

Spec words: "APIs that rely upon AbortController are encouraged to respond to abort() by rejecting any unsettled promise with a new "AbortError" DOMException." spec link.

Motivation: Without rejecting and using well-known value, you might end up with never resolving promises up the chain, or with unexpected errors.

currently The only built-in api that supports AbortContoller is fetch, example:

const c = new AbortController();
const p = fetch('', { signal: c.signal });
c.abort();
try {
    await p;
} catch (e) {
    if (e instanceof DOMException && e.name === "AbortError") {
        // aborted by us, no need to do anything
    } else {
        // real error
        console.error(e)
    }
}

On the browser AbortError is not a special constructor, but an instance of DOMException:
new DOMException('Aborted', 'AbortError'), with name=AbortError and a custom message

@addaleax
Copy link
Member

  • We don't have DOMException in node, so we need to make it instanceof Error

Small correction: We do have DOMException in Node.js:

> new worker_threads.MessageChannel().port1.postMessage(process)
Uncaught:
DOMException [DataCloneError]: function _rawDebug(...args) {
    binding._rawDebug(format.apply(null, args));
  } could not be cloned.

It’s not exposed as a global, but we could change that of course.

@Bnaya
Copy link
Author

Bnaya commented May 24, 2020

Good to know :)
I personaly still prefer using plain Error, and document the difference

@benjamingr benjamingr added the discuss Issues opened for discussions and feedbacks. label May 25, 2020
@jasnell
Copy link
Member

jasnell commented May 25, 2020

It would be fairly easy for us to expose an AbortError using our current internal errors mechanism. I'd prefer not to extend off DOMException tho.

@jasnell
Copy link
Member

jasnell commented Jun 19, 2020

Now that AbortController has landed and uses DOMException with AbortError, we can close this.

@jasnell jasnell closed this as completed Jun 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
discuss Issues opened for discussions and feedbacks.
Projects
None yet
Development

No branches or pull requests

4 participants