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

$.getScript().abort() does not abort its request (as well as $.ajax({ dataType: "script" })) #4912

Open
eugene-lazarev opened this issue Jul 30, 2021 · 12 comments
Labels

Comments

@eugene-lazarev
Copy link

eugene-lazarev commented Jul 30, 2021

Description

  • What do you expect to happen?
    Both $.ajax() and $.getScript() requests are aborted after requesting .abort() method on them.
  • What actually happens?
    $.ajax() aborts well, $.getScript() does not abort its network request. Both of them triggers .fail(failCallback) callback as expected.
  • Which browsers are affected?
    I am testing on latest Chrome 92.0.4515.107 on Mac OS 10.14.6.

Link to test case

https://jsbin.com/kasonuqobu/1/edit?html,js,output

@eugene-lazarev
Copy link
Author

Please check your Network panel in Chrome Developer Tools to get into this bug:

Снимок экрана 2021-07-31 в 3 14 19

@eugene-lazarev
Copy link
Author

eugene-lazarev commented Jul 30, 2021

One more clarification.

.abort() works well on $.ajax() but does not work when set $.ajax({ dataType: "script" })

@eugene-lazarev
Copy link
Author

eugene-lazarev commented Jul 30, 2021

OK, one more.

If we set $.ajax({ url: "...", timeout: 1 }), it aborts well.
If we set $.ajax({ url: "...", dataType: "script", timeout: 1 }), it does not abort request, but triggers .fail() callback as expected.

@eugene-lazarev eugene-lazarev changed the title $.getScript().abort() does not abort its request $.getScript().abort() does not abort its request (as well as $.ajax({ dataType: "script" })) Jul 30, 2021
@mgol
Copy link
Member

mgol commented Jul 31, 2021

Thanks for the report. The script transport uses a script tag to inject JS code and, unfortunately, there's no way to abort a request triggered by adding a script tag.

@mgol mgol closed this as completed Jul 31, 2021
@eugene-lazarev
Copy link
Author

eugene-lazarev commented Jul 31, 2021

Dear @mgol, thanks for the answer. So if it can’t be aborted, it should not trigger fail callbacks, right? But it does trigger. So this inconsistency looks like a problem for me.

@mgol
Copy link
Member

mgol commented Jul 31, 2021

Hmm, that looks inconsistent, indeed. I'll reopen so that the team can discuss this. Thanks!

@mgol mgol reopened this Jul 31, 2021
@mgol mgol added the Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. label Jul 31, 2021
@timmywil timmywil added Ajax and removed Discuss in Meeting Reserved for Issues and PRs that anyone would like to discuss in the weekly meeting. labels Aug 2, 2021
@timmywil
Copy link
Member

timmywil commented Aug 2, 2021

Discussed in the meeting. We're not sure the right solution here. While it is inconsistent that the failure callback gets called, the script execution can't really be aborted. There's an argument that it's best to leave this as-is but add a clearer error message to promise rejection in the script transport abort method, simply stating that it can't be aborted.

@eugene-lazarev
Copy link
Author

Hi @timmywil, thanks for this update. It would be awesome if you clarify what is an argument to call fail callback even when there is no fail event.
As additional, I propose you to add some explanations in jQuery docs about ajaxing scripts. I think it would be great to have some explanations that it can’t be aborted.
Thanks again!

@timmywil
Copy link
Member

timmywil commented Aug 2, 2021

The fail event event is that it fails to abort. To silently fail seems worse and would lead to unexpected behavior. It would be best not to call abort at all, but we can fix that with the right error message.

@timmywil
Copy link
Member

timmywil commented Aug 2, 2021

That said, we can add some clarification to the docs about this. While all ajax requests have abort methods, not all of them are useful.

@eugene-lazarev
Copy link
Author

The fail event event is that it fails to abort

I’m not sure if I understand it well.
Do you mean that fail callback in this case triggers because .abort() can’t abort this request?
I mean... I believe that fail callback triggers only if my ajax request is experiencing some problems (timeout, wrong server response, etc...) and not because I call some broken method over that.

@timmywil
Copy link
Member

timmywil commented Aug 2, 2021

my ajax request is experiencing some problems

I see what you mean. This is true most of the time, but in the end it's just a Promise/Deferred. It can be rejected for other errors as well, including logic errors, errors in done/then callbacks, and, in this case, an error attempting to change the request. Another idea was to throw an error in the abort method (instead of rejecting the promise), but I think that has more potential to break existing code. It would just halt the stack. My argument was that fail callbacks are already getting called; they might as well get called with a more helpful error message.

I don't know if there's a perfect solution. But imagine not calling any callbacks after calling abort and later the script still gets executed. That seems like the worst of the 3 options so far.

@jquery jquery deleted a comment Apr 11, 2022
@jquery jquery deleted a comment Apr 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants