What is @aws-sdk/abort-controller?
The @aws-sdk/abort-controller npm package provides a mechanism to abort one or more Web requests before they have completed. It is primarily used to enhance the management of API requests, particularly in environments where network requests can hang or need to be manually terminated to manage resources effectively.
Abort HTTP requests
This code sample demonstrates how to use the AbortController to abort an HTTP fetch request. The 'signal' is passed to the fetch request, and the request can be aborted by calling 'controller.abort()'.
const { AbortController } = require('@aws-sdk/abort-controller');
const controller = new AbortController();
const signal = controller.signal;
fetch('https://api.example.com/data', { signal })
.then(response => response.json())
.catch(err => {
if (err.name === 'AbortError') {
console.log('Fetch aborted');
} else {
console.error('Fetch error:', err);
}
});
// Abort the request
controller.abort();