Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Supplying a reason for AbortController?

This code aborts the request after 10 ms:

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


fetch('https://jsonplaceholder.typicode.com/todos/1', {
        signal
    })
    .then(response => response.json())
    .then(data => {
        console.log('Data:', data);
    })
    .catch(err => {
        console.log(err)
        if (err.name === 'AbortError')....

    });

 
setTimeout(() => {
    controller.abort();
 
}, 10);

Output :

DOMException: signal is aborted without reason

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Question:
If it says "without a reason," is it possible to supply a reason?
Because otherwise, the message is a bit strange, as if I could supply a reason but didn’t.

>Solution :

You can supply a reason as an argument to controller.abort(), like controller.abort("took longer than 10 milliseconds")

From MDN:

abort()

abort(reason)

reason (optional): The reason why the operation was aborted, which can be any JavaScript value. If not specified, the reason is set to "AbortError" DOMException.

https://developer.mozilla.org/en-US/docs/Web/API/AbortController/abort

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading