Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why AbortController is 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.

like image 729
karolis2017 Avatar asked Apr 17 '19 01:04

karolis2017


2 Answers

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).

like image 155
Philipp Claßen Avatar answered Sep 17 '22 08:09

Philipp Claßen


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!).

like image 22
plainOldNerd Avatar answered Sep 20 '22 08:09

plainOldNerd