js stop old api calling when calling new api
There are several ways to stop old API calling when calling a new API in JavaScript. One approach is to use the AbortController API, which allows you to cancel requests that are in progress.
Here is an example of how to use the AbortController API:
const abortController = new AbortController();
const signal = abortController.signal;
fetch('https://old-api.com', { signal })
.then(response => {
// handle the response
})
.catch(error => {
// handle the error
});
// Call the new API
fetch('https://new-api.com')
.then(response => {
// handle the response
})
.catch(error => {
// handle the error
});
// Cancel the old API call when the new API is called
abortController.abort();
In this example, we create a new AbortController and pass its signal to the old API call using the fetch API. We then call the new API using the fetch API again. Finally, we call abort() on the AbortController, which cancels the old API call and prevents it from completing.
原文地址: https://www.cveoy.top/t/topic/bcN9 著作权归作者所有。请勿转载和采集!