lllretry
v1.0.1
Published
Zero-dependency, lightweight async retry utility with exponential backoff
Readme
LLLRetry
🇬🇧 English
Zero-dependency, lightweight async retry utility with exponential backoff for Node.js (ESM).
Features
- 🚀 Zero Dependencies: Written in pure JavaScript, extremely lightweight.
- ⚡️ ESM Only: Optimized for the modern ECMAScript Module system.
- 📈 Exponential Backoff: Built-in backoff mechanism to prevent server overload.
- 💡 TypeScript Support: Full type inference and auto-completion included.
Installation
npm install lllretryUsage
import { retry } from 'lll-retry';
// 1. Basic usage (3 retries, exponential backoff enabled)
async function fetchData() {
const data = await retry(async () => {
const response = await fetch('[https://api.example.com/data](https://api.example.com/data)');
if (!response.ok) throw new Error('API Error');
return response.json();
});
console.log(data);
}
// 2. Custom options (5 retries, 500ms initial delay, backoff disabled)
async function connectDB() {
await retry(async () => {
await database.connect();
}, {
retries: 5,
delay: 500,
backoff: false
});
}API
retry(fn, [options])
fn : The asynchronous function to execute.
- options
- retries : Maximum number of retry attempts upon failure. (default: 3)
- delay : Base delay time (ms) between retries. (default: 1000)
- backoff : Whether to enable exponential backoff. If true, the wait time increases by delay * 2^n. (default: true)
🇰🇷 한국어
Node.js(ESM) 환경을 위한 의존성 없는 초경량 비동기 재시도(Retry) 유틸리티. 지수 백오프(Exponential backoff)를 기본 지원합니다.
주요 기능
🚀 의존성 제로 (Zero Dependencies): 순수 JavaScript로 작성되어 매우 가볍습니다.
⚡️ ESM 전용: 최신 모듈 시스템에 최적화되어 있습니다.
📈 지수 백오프 지원: 서버 과부하를 막기 위해 재시도 대기 시간을 점진적으로 늘려줍니다.
💡 TypeScript 지원: 완벽한 타입 추론과 자동완성을 위한 타입 정의(d.ts)를 포함합니다.
설치 방법
npm install LLLRetry사용 방법
import { retry } from 'lll-retry';
// 1. 기본 사용법 (3회 재시도, 지수 백오프 적용)
async function fetchData() {
const data = await retry(async () => {
const response = await fetch('[https://api.example.com/data](https://api.example.com/data)');
if (!response.ok) throw new Error('API Error');
return response.json();
});
console.log(data);
}
// 2. 옵션 커스텀 (5회 재시도, 초기 대기시간 500ms, 백오프 끄기)
async function connectDB() {
await retry(async () => {
await database.connect();
}, {
retries: 5,
delay: 500,
backoff: false
});
}API 명세서
retry(fn, [options])
- fn : 실행할 비동기 함수.
- options
- retries : 실패 시 최대 재시도 횟수. (기본값: 3)
- delay : 실패 후 다음 시도까지의 기본 대기 시간(ms). (기본값: 1000)
- backoff : 지수 백오프 활성화 여부. true일 경우 대기 시간이 delay * 2^n으로 늘어납니다. (기본값: true)
