Techloghub - Dev tricks

Stop writing this boilerplate to timeout a fetch call:
const controller = new AbortController()setTimeout(() => controller.abort(), 5000)fetch(url, { signal: controller.signal })
Use AbortSignal.timeout() instead:
fetch(url, { signal: AbortSignal.timeout(5000) })
One line. Auto-cancels after 5 seconds. No manual timer, no manual controller.
Works in all modern browsers and Node 17.3+.
Combine multiple signals with AbortSignal.any() if you need timeout + manual cancel together.

Stickers
Loading stickers...

Comments

No comments yet

Be the first to share your thoughts.