typestar

Fetch con reintentos en JavaScript

Reintenta una petición fallida con espera creciente: las redes fallan un momento y luego dejan de fallar.

async function fetchRetry(url, attempts = 3) {
  for (let i = 0; i < attempts; i++) {
    try {
      const res = await fetch(url);
      if (res.ok) return res.json();
      throw new Error(`HTTP ${res.status}`);
    } catch (err) {
      if (i === attempts - 1) throw err;
    }
  }
}

Palabras clave y builtins usados aquí

El intento, en números

Líneas
11
Caracteres a escribir
246
Tokens
84
Ritmo de tres estrellas
110 tpm

Al ritmo de tres estrellas de 110 tokens por minuto, este intento toma unos 46 segundos.

Escribe este fragmento

Paso 4 de 4 en Cancelar y limitar; paso 18 de 19 en Promesas y async.

← Anterior Siguiente →