JavaScript · Middle · Question 39 of 75
How do you cancel a fetch request?
- ✓Correct answer: Pass a
signalfrom anAbortControllerand callcontroller.abort() - 2Call the
cancel()method on the promise returned byfetch - 3Set the promise to
null- the garbage collector stops the request
Explanation
After abort(), the promise rejects with an AbortError. The same signal can be passed to several requests and to addEventListener. For a timeout there's AbortSignal.timeout(5000). Promises have no cancel method.