typestar

Reintento async en TypeScript

Reintenta una operación async con backoff, tipada según lo que resuelve.

async function retry<T>(
  fn: () => Promise<T>,
  attempts = 3,
): Promise<T> {
  let lastError: unknown;
  for (let i = 0; i < attempts; i++) {
    try {
      return await fn();
    } catch (err) {
      lastError = err;
    }
  }
  throw lastError;
}

Palabras clave y builtins usados aquí

El intento, en números

Líneas
14
Caracteres a escribir
218
Tokens
72
Ritmo de tres estrellas
105 tpm

Al ritmo de tres estrellas de 105 tokens por minuto, este intento toma unos 41 segundos.

Escribe este fragmento

Paso 5 de 5 en Tiempo y memoria; paso 18 de 19 en Clases y patrones.

← Anterior Siguiente →