Abort asynchronous task
How to abort Asynchronous task
There’s a special built-in object for such purposes: AbortController
. It can be used to abort not only fetch
, but other asynchronous tasks as well.
The usage is very straightforward:
The AbortController object
Create a controller:
let controller = new AbortController();
A controller is an extremely simple object.
- It has a single method
abort()
, - And a single property
signal
that allows to set event listeners on it.
When abort()
is called:
controller.signal
emits the"abort"
event.controller.signal.aborted
property becomestrue
.
Generally, we have two parties in the process:
- The one that performs a cancelable operation, it sets a listener on
controller.signal
. - The one that cancels: it calls
controller.abort()
when needed.
When a fetch is aborted, its promise rejects with an error AbortError
, so we should handle it, e.g. in try..catch
.
Fetch메소드를 실행시키면 promise로 관리.
let controller = new AbortController();
let signal = controller.signal;
// The party that performs a cancelable operation
// gets the "signal" object
// and sets the listener to trigger when controller.abort() is called
signal.addEventListener('abort', () => alert("abort!"));
// The other party, that cancels (at any point later):
controller.abort(); // abort!
// The event triggers and signal.aborted becomes true
alert(signal.aborted); // true
with the fetch()
// abort in 1 second
let controller = new AbortController();
setTimeout(() => controller.abort(), 1000);
try {
let response = await fetch('/article/fetch-abort/demo/hang', {
signal: controller.signal
});
} catch(err) {
if (err.name == 'AbortError') { // handle abort()
alert("Aborted!");
} else {
throw err;
}
}
Author And Source
이 문제에 관하여(Abort asynchronous task), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@hqillz/Abort-asynchronous-task저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)