javascript
42 lines · 8 steps
A resilient fetch wrapper with retries and timeout
How to merge default request options and wrap fetch with retry and abort-based timeout logic.
Explained by
highlit
1const DEFAULT_OPTIONS = {
2 method: 'GET',
3 timeout: 5000,
4 retries: 3,
5 headers: {
6 Accept: 'application/json',
7 'Content-Type': 'application/json',
8 },
9 cache: 'no-store',
10};
11
12function resolveRequestOptions(overrides = {}) {
13 const { headers: overrideHeaders, ...restOverrides } = overrides;
14
15 return {
16 ...DEFAULT_OPTIONS,
17 ...restOverrides,
18 headers: {
19 ...DEFAULT_OPTIONS.headers,
20 ...overrideHeaders,
21 },
22 };
23}
24
25export async function apiFetch(url, overrides) {
26 const { timeout, retries, ...fetchOptions } = resolveRequestOptions(overrides);
27
28 for (let attempt = 0; attempt <= retries; attempt++) {
29 const controller = new AbortController();
30 const timer = setTimeout(() => controller.abort(), timeout);
31
32 try {
33 const response = await fetch(url, { ...fetchOptions, signal: controller.signal });
34 if (!response.ok && attempt < retries) continue;
35 return response;
36 } catch (err) {
37 if (attempt === retries) throw err;
38 } finally {
39 clearTimeout(timer);
40 }
41 }
42}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Nested config objects need a deep merge so overriding one header doesn't wipe the rest.
- 2AbortController plus setTimeout gives fetch the request timeout it lacks natively.
- 3A retry loop must distinguish the final attempt so it can surface the real error instead of swallowing it.
Related explainers
go
package httpx import ( "net/http"
A retrying HTTP transport in Go
http
middleware
retry
Intermediate
8 steps
javascript
import { parsePhoneNumberFromString } from 'libphonenumber-js'; export class PhoneValidationError extends Error { constructor(message, code) {
Normalizing phone numbers with typed errors
validation
error-handling
custom-errors
Intermediate
6 steps
javascript
const express = require('express'); const { z } = require('zod'); const router = express.Router();
Validating query params with Zod in Express
validation
schema-parsing
query-building
Intermediate
9 steps
rust
use std::time::Duration; use axum::{extract::State, http::StatusCode, response::IntoResponse, Json}; use serde::Serialize;
A timeout-guarded health check in Axum
health-check
timeout
error-handling
Intermediate
6 steps
javascript
const RESERVED_SLUGS = new Set(['new', 'edit', 'admin', 'api']); function slugify(title) { return title
Generating URL-safe unique slugs
string-normalization
regex
deduplication
Intermediate
9 steps
javascript
class EventEmitter { constructor() { this.listeners = new Map(); }
Building an EventEmitter in JavaScript
pub-sub
closures
map
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/a-resilient-fetch-wrapper-with-retries-and-timeout-explained-javascript-4677/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.