Skip to content

AbortController/AbortSignal support #1297

@lukasolson

Description

@lukasolson

Currently, the TransportRequestPromise type is an extension of the built-in Promise type, which contains an abort method to programmatically cancel the request. Extending the native Promise makes it difficult to chain/wrap or use async/await`.

As an alternative (or additional) approach, I'd like to suggest using the AbortController/AbortSignal approach used natively (supported in most modern browsers, polyfill available) by fetch:

const controller = new AbortController();

const response = await esClient.search(params, {
  signal: controller.signal
});

// Then, if we need to abort for some reason, maybe in an event handler:
controller.abort();

It sounds like @delvedor has been considering this since it is likely to land natively in Node [1]. From our conversations:

That’s something I’ve been thinking for a while, as a similar api is landing to node core.
Unfortunately we can’t drop the support for the .abort method as it would be a breaking change, but we can support both and deprecate the old one. I’ve already implemented it in userland libraries (https://github.com/nodejs/undici), the main reason why the client has an .abort method, is because the legacy one had it. I want to see how Node.js will end up supporting this, to avoid creating an api not compatible with the rest of the ecosystem.

[1] nodejs/node#33527

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions