| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export default function() { | ||
| this.signal = { onabort: () => {} }; | ||
| this.abort = () => { | ||
| this.signal.onabort(); | ||
| }; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| Expand Up | @@ -17,6 +17,9 @@ export default function(url, options) { | |
|
|
||
| request.onerror = reject; | ||
|
|
||
| if (options.signal) options.signal.onabort = () => { request.abort(); } | ||
|
Comment thread
Copy link
Copy Markdown
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualityif signal is already aborted it should reject promise, first thing: https://dom.spec.whatwg.org/#aborting-ongoing-activities-spec-example
Sorry, something went wrong.
All reactions
|
||
| request.onabort = () => reject(new DOMException('The user aborted a request.')); | ||
|
Comment thread
Copy link
Copy Markdown
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualitythis is to simulate what Chrome does, but i don't insist on it
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualityper the spec (even that chrome exception has a name of AbortError): reject(new DOMException('Aborted', 'AbortError'));https://dom.spec.whatwg.org/#aborting-ongoing-activities
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityAlso, don't think DOMException is globally available: https://developer.mozilla.org/en-US/docs/Web/API/DOMException
Sorry, something went wrong.
All reactions
Copy link
Copy Markdown
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low QualityI wonder what browsers don't have it. I can't find data about this in caniuse. Shall we use Error instead?
Sorry, something went wrong.
All reactions
|
||
|
|
||
| request.send(options.body || null); | ||
|
|
||
| function response() { | ||
| Expand Down | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import fetch from '../src/index.mjs'; | ||
| import AbortController from '../src/AbortController.mjs'; | ||
|
|
||
| describe('AbortController', () => { | ||
| it('should be a function', () => { | ||
| expect(AbortController).toEqual(expect.any(Function)); | ||
| }); | ||
|
|
||
| describe('AbortController()', () => { | ||
| let xhr; | ||
|
|
||
| beforeEach(() => { | ||
| xhr = { | ||
| setRequestHeader: jest.fn(), | ||
| getAllResponseHeaders: jest.fn().mockReturnValue('X-Foo: bar\nX-Foo:baz'), | ||
| open: jest.fn(), | ||
| send: jest.fn(), | ||
| abort: jest.fn(() => xhr.onabort({ type: 'abort' })) , | ||
| status: 200, | ||
| statusText: 'OK', | ||
| responseText: '{"a":"b"}', | ||
| responseURL: '/foo?redirect' | ||
| }; | ||
|
|
||
| global.XMLHttpRequest = jest.fn(() => xhr); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| delete global.XMLHttpRequest; | ||
| }); | ||
|
|
||
| it('handles abort', () => { | ||
| let controller = new AbortController(); | ||
| let signal = controller.signal; | ||
| let p = fetch('/foo', { signal }) | ||
| .then(() => {}) | ||
| .catch((e) => { | ||
| expect(e.message).toEqual('The user aborted a request.'); | ||
| }); | ||
|
|
||
| controller.abort(); | ||
|
|
||
| return p; | ||
| }); | ||
| }); | ||
| }); |
| Back | FazBrowse Home | New Git URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Qualitysignal has an aborted flag indicating it's been aborted or not, which can be handy:
https://dom.spec.whatwg.org/#abortsignal-aborted-flag
https://github.com/mo/abortcontroller-polyfill/blob/master/src/abortcontroller.js#L42
https://github.com/mysticatea/abort-controller/blob/master/src/abort-signal.mjs#L28
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.