karolis2017
karolis2017

Reputation: 2415

Why is AbortController not defined?

I want to be able to cancel requests with fetch API and use new AbortController() Unfortunately I get an error in the console: AbortController is not defined

//  this.aborter = new XMLHttpRequest()  no error
    this.aborter = new AbortController() error

What might be the reason? I'm using just vanilla JS without any dependencies.

Upvotes: 11

Views: 24705

Answers (2)

plainOldNerd
plainOldNerd

Reputation: 305

try

this.aborter = new window.AbortController();

I found that on Chrome (v77) it didn't recognise AbortController with specifying it as a window property.

Also, after you call

this.aborter.abort()

you may need to re-initialise

this.aborter = new window.AbortController();

or future fetch statements won't work (the status will be aborted and it will throw an error!).

Upvotes: 8

Philipp Claßen
Philipp Claßen

Reputation: 43950

The MDN documentation on AbortController includes an up-to-date table of supported browsers.

The API is still marked as experimental, although it should be well supported in current browsers. Firefox has it since November 2017 (FF 57), and Chrome followed on April 2018 (Chrome 66).

Upvotes: 5

Related Questions