Skip to content

Instantly share code, notes, and snippets.

@VldMrgnn
Last active June 19, 2024 15:43
Show Gist options
  • Save VldMrgnn/b7f33ecea4c839bceba7c1b8e8cafe4a to your computer and use it in GitHub Desktop.
Save VldMrgnn/b7f33ecea4c839bceba7c1b8e8cafe4a to your computer and use it in GitHub Desktop.
effection fetch timeout
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