最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to prevent the AbortError: signal is aborted without reason - Stack Overflow

programmeradmin0浏览0评论

I am getting a AbortError: signal is aborted without reason.

In the past I believe I was able to call abort on the signal even after fetch returned but now I'm getting this error. Maybe it is because I'm calling it twice?

Is there a way to prevent this error? I've checked if signal.aborted is true but that would be checking if it was aborted not if the request was completed. Is there a property with the controller or signal that I can use to check if the fetch is done or can I simply catch the error (since it seems like it's more of a warning) and discard it?


class MyClass {

async send(url) {
   const controller = new AbortController();
   const signal = controller.signal;
   if (options == null) { options = {} }
   if (options.signal == null) { options.signal = signal };

   this.controllers.set(requestId, controller);
   
   // makes a fetch but not really 
   // gets the preflight response not full response
   var response = await fetch(url, options);

   // gets the full responses and returns it as text
   var text = await response.text();

   return response;
}

cancel() {
   if (this.controllers) {
      this.controllers.forEach((value, key, map) => {
          if (value.signal.aborted == false) {
              value.abort(); // error here
          }
      });
   }
}

async checkProgress() {
    // make a call and check results
    var data = await send(url);
    if (dataplete) {
        thisplete = true;
        this.cancel(); // cancel any lingering or requests in progress
    }

}

async doThing() {
    // something like this not real code
    var response = await send("abc");
    while (thisplete == false) {
        await checkProgress("progress"); // sets thisplete to true when complete
    }
}

}

var myClass = new MyClass();
await myClass.doThing();
console.log("Thing done")

UPDATE:
I just noticed this comment on the abort() page on MDN.

Note: When abort() is called, the fetch() promise rejects with an AbortError.

So is this an error or not?

MDN AbortController

发布评论

评论列表(0)

  1. 暂无评论