denoify
Version:
For NPM module authors that would like to support Deno but do not want to write and maintain a port.
23 lines (18 loc) • 526 B
text/typescript
import fetch from "node-fetch";
export async function fetchWithTimeout(url: string, options: { timeout: number }) {
const controller = new AbortController();
const timer = setTimeout(() => {
controller.abort();
}, options.timeout); // Timeout set to 8000 ms
let fetchResponse;
try {
fetchResponse = await fetch(url, {
signal: controller.signal
});
} catch (err) {
throw err;
} finally {
clearTimeout(timer);
}
return fetchResponse;
}