
Security News
npm Adopts OIDC for Trusted Publishing in CI/CD Workflows
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
smart-fetch-retry
Advanced tools
Una utilidad ligera para realizar peticiones fetch con reintentos automáticos y delay personalizable, compatible con Node.js y navegadores.
Una utilidad ligera para realizar peticiones HTTP con reintentos automáticos y soporte para Node.js y navegadores.
Perfecto para manejar solicitudes poco confiables, como llamadas a APIs externas que pueden fallar temporalmente.
node-fetch
en Node.jsasync/await
y también con .then()AbortController
(nuevo en v1.1.0)npm install smart-fetch-retry
Nota: En Node.js 16+ se requiere instalar
node-fetch
si no existe soporte nativo defetch
:
npm install node-fetch
import { smartFetchRetry } from "smart-fetch-retry";
const url = "https://api.escuelajs.co/api/v1/products";
async function main() {
try {
const { promise } = smartFetchRetry(url, { method: "GET" }, 3, 1500, 5000);
const response = await promise;
const data = await response.json();
console.log("Datos recibidos:", data);
} catch (error) {
console.error("Error:", error.message);
}
}
main();
El último parámetro define el tiempo máximo (en milisegundos) antes de cancelar automáticamente la petición:
const { promise } = smartFetchRetry("https://api.example.com/data", {}, 3, 1500, 3000);
Puedes cancelar manualmente la petición usando controller.abort():
const { promise, controller } = smartFetchRetry("https://api.example.com/data", {}, 3, 1500, 5000);
promise
.then(res => res.json())
.then(data => console.log("Datos:", data))
.catch(err => console.error("Error:", err.message));
setTimeout(() => {
controller.abort();
console.log("Petición cancelada manualmente");
}, 2000);
Parámetro | Tipo | Por defecto | Descripción |
---|---|---|---|
url | string | Requerido | URL de la petición. |
options | object | {} | Opciones para fetch (method , headers , body ). |
retries | number | 3 | Número de intentos antes de fallar. |
delay | number | 1000 ms | Tiempo de espera entre cada intento (en ms). |
timeout | number | 5000 ms | Tiempo máximo antes de cancelar la petición automáticamente. |
const { promise } = smartFetchRetry(
"https://api.example.com/data",
{
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name: "John" }),
},
5,
2000
);
const data = await response.json();
console.log(data);
Si después de todos los reintentos la solicitud sigue fallando, la función lanza un error:
try {
const { promise } = smartFetchRetry("https://api.fake.com/fail", {}, 2, 1000);
await promise;
} catch (err) {
console.error(err.message); // "Fallo después de 2 intentos: ..."
}
AbortController
(implementado en v1.1.0).MIT © 2025 Miguel Ignacio González
FAQs
Una utilidad ligera para realizar peticiones fetch con reintentos automáticos y delay personalizable, compatible con Node.js y navegadores.
The npm package smart-fetch-retry receives a total of 134 weekly downloads. As such, smart-fetch-retry popularity was classified as not popular.
We found that smart-fetch-retry demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
npm now supports Trusted Publishing with OIDC, enabling secure package publishing directly from CI/CD workflows without relying on long-lived tokens.
Research
/Security News
A RubyGems malware campaign used 60 malicious packages posing as automation tools to steal credentials from social media and marketing tool users.
Security News
The CNA Scorecard ranks CVE issuers by data completeness, revealing major gaps in patch info and software identifiers across thousands of vulnerabilities.