How does AbortController work under the hood?

1.4k Views Asked by At

From this link it says

This API is provided by the DOM standard, and that's the entire API. It's deliberately generic so it can be used by other web standards and JavaScript libraries.

I assume every browser has its own implementation. However, it does not give a generic answer how this is handled under the hood.

This blog post explains how HTTP is stateless and someone with a goal of canceling a request should choose another protocol like gRPC.

Here is an example from the first link which is identical to what I am trying to do with my project. In my case I am making a put request for a file upload. (I am not interested in handling cancellation on the backend)

const controller = new AbortController();
const signal = controller.signal;

setTimeout(() => controller.abort(), 5000);

fetch(url, { signal }).then(response => {
    return response.text();
}).then(text => {
    console.log(text);
});

One last link I also found unclear from the MDN docs. Any help explaining the behaviour would be helpful. Thanks in advance.

AbortController.abort() Aborts a DOM request before it has completed. This is able to abort fetch requests, consumption of any response bodies, and streams.

0

There are 0 best solutions below