MSNCodeSNIPPETS← msncode.dev
snippets/nodejs

Fetch with Timeout

Add a timeout to fetch requests using AbortController

nodejsjavascriptfetchtimeout
JAVASCRIPT
const ctrl = new AbortController();
setTimeout(() => ctrl.abort(), 5000);
const r = await fetch(url, { signal: ctrl.signal });

More nodejs snippets

Check if Directory Exists

Check if a path exists and is a directory

Get Current Directory Path

Get the directory of the currently running script

Safe JSON Parse

Parse JSON without try-catch crashing your app

Read Environment Variable

Access environment variables in Node.js