Fetch With Timeout
NodeJS
Hard
4 views
Problem Description
Use AbortController to timeout a fetch call.
Output Format
Print TIMEOUT or OK.
Constraints
Use Node fetch when available.
Official Solution
async function run() {
const ac = new AbortController();
const id = setTimeout(() => ac.abort(), 30);
try {
await fetch('https://example.com', { signal: ac.signal });
console.log('OK');
} catch (e) {
console.log('TIMEOUT');
} finally {
clearTimeout(id);
}
}
run();
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!