Skip to content

Conversation

@aron
Copy link
Contributor

@aron aron commented Feb 5, 2025

This PR adds support for passing AbortSignal to all API methods that make HTTP requests, these are passed directly into the native fetch() implementation, so it's up to the user to handle the AbortError raised, if any.

const controller = new AbortController();
try {
  const prediction = await replicate.predictions.create({
    version: 'xyz',
    ...,
    signal: controller.signal,
  });
} catch (err) {
  if (err instanceof DOMException && err.name === "AbortError") {
    ...
  }
}

The paginate function also checks to see whether the signal was aborted before proceeding to the next iteration. If so it returns immediately to avoid making a redundant fetch call.

This allows the client to take advantage of various frameworks that provide an AbortSignal instance to tear down any in flight requests.

Copy link
Member

@erbridge erbridge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor comment about the docs, but otherwise, I think this looks good.

@aron aron force-pushed the support-abort-signal branch from 6c7e05b to 6c0a5c8 Compare March 26, 2025 17:22
@aron aron merged commit ac5caba into main Mar 26, 2025
19 checks passed
@aron aron deleted the support-abort-signal branch March 26, 2025 17:30
@renet
Copy link

renet commented Apr 10, 2025

@aron This has not been released yet, right? When will the new version containing this feature be released? 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants