Run ❯
Get your
own Node
server
❯
Run Code
Ctrl+Alt+R
Change Orientation
Ctrl+Alt+O
Change Theme
Ctrl+Alt+D
Go to Spaces
Ctrl+Alt+P
// Promise.race - Timeout Demo const timeout = (ms) => new Promise((_, reject) => setTimeout(() => reject(new Error(`Timeout after ${ms}ms`)), ms)); async function fetchWithTimeout(promise, timeoutMs = 50) { return Promise.race([ promise, timeout(timeoutMs).then(() => { throw new Error(`Request timed out after ${timeoutMs}ms`); }), ]); } async function run() { try { // Simulate a slower promise to ensure timeout path const slow = new Promise((resolve) => setTimeout(() => resolve({ ok: true }), 200)); await fetchWithTimeout(slow, 50); console.log('Should not reach here'); } catch (error) { console.log(error.message); } } run();
Expected console output:
Request timed out after 50ms