Last active
October 22, 2023 13:19
-
-
Save susilolab/a864012079160ee14266c177da0f9515 to your computer and use it in GitHub Desktop.
Request ajax dengan timeout pure js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const fetchTimeout = (url, ms, {signal, ...options} = {}) => { | |
const controller = new AbortController() | |
const promise = fetch(url, {signal: controller.signal, ...options}) | |
if (signal) signal.addEventListener('abort', () => controller.abort()) | |
const timeout = setTimeout(() => controller.abort(), ms) | |
return promise.finally(() => clearTimeout(timeout)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment