Last active
June 19, 2024 15:43
-
-
Save VldMrgnn/b7f33ecea4c839bceba7c1b8e8cafe4a to your computer and use it in GitHub Desktop.
effection fetch timeout
This file contains hidden or 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
function* fetchRace(endpoint:string, timeout: number) { | |
let result: Response | null = null; | |
function* fetcher(endpoint: string, signal: AbortSignal) { | |
try { | |
result = yield* call(() => fetch(endpoint, { signal })); | |
} catch (error) { | |
console.error("fetchRace error", error); | |
} | |
} | |
const controller = new AbortController(); | |
yield* spawn(()=>fetcher(endpoint, controller.signal)); | |
yield* sleep(timeout); | |
if (!result) { | |
controller.abort('time out'); | |
} | |
// | |
console.log('result', result); | |
} | |
fetchRace('/rest/api/s1', 1300); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment