DEV Community

Discussion on: Aborting a fetch request

Collapse
 
taufik_nurrohman profile image
Taufik Nurrohman • Edited

Comparing between fetch and XMLHttpRequest for extra information:

Fetch

const controller = new AbortController;

fetch('/foo/bar', {
    signal: controller.signal
}).then(response => {
    if (response.ok) {
        alert(response.text());
    }
});

// Abort!
controller.abort();

XHR

const xhr = new XMLHttpRequest;

xhr.addEventListener('load', () => {
    if (xhr.status === 200) {
        xhr.responseType = 'text';
        alert(xhr.response);
    }
});

xhr.open('GET', '/foo/bar');
xhr.send();

// Abort!
xhr.abort();