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

Can it be achieved to interrupt or cancel the response during the response process #62

Open
Pan-yongyong opened this issue Oct 19, 2023 · 4 comments

Comments

@Pan-yongyong
Copy link

Can it be achieved to interrupt or cancel the response during the response process

@cobbweb
Copy link

cobbweb commented Nov 7, 2023

Do you mean cancelling an active request from the client? If so, you can provide a signal from an AbortController

@jgarplind
Copy link

Do you mean cancelling an active request from the client? If so, you can provide a signal from an AbortController

You don't happen to have an example of this working by any chance?

I understand that this is default fetch functionality that is supposedly not impacted by this library, but for me, providing the signal seems to have no impact, so I had to resort to checking for it in onopen like so:

async onopen(response) {
          // throw AbortError manually to force abort.
          if (ctrl.signal.aborted) {
            throw new AbortError();
          }

@cobbweb
Copy link

cobbweb commented Nov 20, 2023

It was pretty straight forward for me. My actual code is too complicated for a simple example but it boils down to something like:

const abortController = new AbortController()
fetchEventSource(someUrl, {
  signal: abortController.signal,
  onmessage(ev) {
    const data = JSON.parse(ev.data) as MyData;
    console.log(data);
  },
})
abortController.abort(); // this should cancel & close your request

If you're using React.js or similar, make sure you've got the same abortController instance reference.

@Gc-Peanut
Copy link

中断需要你在 onmessage(){ if(needStop){throw Error()} }
在 onerror(){ return err}

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

No branches or pull requests

4 participants