11[ ![ Build] ( https://github.com/suhaotian/async-plugins/actions/workflows/check.yml/badge.svg )] ( https://github.com/suhaotian/async-plugins/actions/workflows/check.yml )
2- [ ![ Size
] ( https://deno.bundlejs.com/[email protected] .1 &badge=detailed&treeshake=%5B%7B+default+%7D%5D )] ( https://bundlejs.com/?q=async-plugins%400.0.1 &treeshake=%5B%7B+default+%7D%5D ) 2+ [ ![ Size
] ( https://deno.bundlejs.com/[email protected] .2 &badge=detailed&treeshake=%5B%7B+default+%7D%5D )] ( https://bundlejs.com/?q=async-plugins%400.0.2 &treeshake=%5B%7B+default+%7D%5D ) 33[ ![ npm version] ( https://badgen.net/npm/v/async-plugins?color=green )] ( https://www.npmjs.com/package/async-plugins )
44![ Downloads] ( https://img.shields.io/npm/dm/async-plugins.svg?style=flat )
55![ typescript] ( https://badgen.net/badge/icon/typescript?icon=typescript&label&color=blue )
@@ -41,25 +41,25 @@ Perfect for handling flaky API calls or network operations:
4141import { createAsyncRetry } from ' async-plugins' ;
4242
4343const fetchWithRetry = createAsyncRetry ({
44- retries: 3 , // Try up to 3 times
45- minTimeout: 1000 , // Start with 1s delay
46- maxTimeout: 10000 , // Cap at 10s delay
47- factor: 2 , // Double the delay each time
48- jitter: true , // Add randomness to prevent thundering herd
49- shouldRetry : (error ) => { // Only retry on network/5xx errors
50- return error . name === ' NetworkError ' ||
51- (error .status && error .status >= 500 );
44+ retries: 3 , // Try up to 3 times
45+ minTimeout: 1000 , // Start with 1s delay
46+ maxTimeout: 10000 , // Cap at 10s delay
47+ factor: 2 , // Double the delay each time
48+ jitter: true , // Add randomness to prevent thundering herd
49+ shouldRetry : (error ) => {
50+ // Only retry on network/5xx errors
51+ return error . name === ' NetworkError ' || (error .status && error .status >= 500 );
5252 },
5353 onRetry : (error , attempt ) => {
5454 console .warn (` Retry attempt ${attempt } after error: ` , error );
55- }
55+ },
5656});
5757
5858// Example: Fetch user data with retries
5959const getUserData = async (userId : string ) => {
6060 try {
61- const response = await fetchWithRetry (() =>
62- fetch (` /api/users/${userId } ` ).then (r => r .json ())
61+ const response = await fetchWithRetry (() =>
62+ fetch (` /api/users/${userId } ` ).then (( r ) => r .json ())
6363 );
6464 return response ;
6565 } catch (error ) {
@@ -78,8 +78,8 @@ Optimize expensive operations and API calls with smart caching:
7878import { createAsyncCache } from ' async-plugins' ;
7979
8080const cache = createAsyncCache ({
81- ttl: 300000 , // Cache for 5 minutes
82- maxSize: 1000 , // Store up to 1000 items
81+ ttl: 300000 , // Cache for 5 minutes
82+ maxSize: 1000 , // Store up to 1000 items
8383 staleWhileRevalidate: true , // Return stale data while refreshing
8484});
8585
@@ -111,7 +111,7 @@ Prevent duplicate API calls and redundant operations:
111111import { createAsyncDedupe } from ' async-plugins' ;
112112
113113const dedupe = createAsyncDedupe ({
114- timeout: 5000 , // Auto-expire after 5s
114+ timeout: 5000 , // Auto-expire after 5s
115115 errorSharing: true , // Share errors between duplicate calls
116116});
117117
@@ -123,8 +123,8 @@ const fetchUserData = dedupe(async (userId: string) => {
123123
124124// Multiple simultaneous calls with same ID
125125const [user1, user2] = await Promise .all ([
126- fetchUserData (' 123' ), // Makes API call
127- fetchUserData (' 123' ), // Uses result from first call
126+ fetchUserData (' 123' ), // Makes API call
127+ fetchUserData (' 123' ), // Uses result from first call
128128]);
129129
130130// Check if operation is in progress
@@ -141,14 +141,14 @@ Control concurrency and manage resource usage:
141141import { createAsyncQueue } from ' async-plugins' ;
142142
143143const queue = createAsyncQueue ({
144- concurrency: 2 , // Process 2 tasks at once
145- autoStart: true , // Start processing immediately
144+ concurrency: 2 , // Process 2 tasks at once
145+ autoStart: true , // Start processing immediately
146146});
147147
148148// Example: Rate-limit API calls
149149const processUsers = async (userIds : string []) => {
150150 const results = await queue .addAll (
151- userIds .map (id => async () => {
151+ userIds .map (( id ) => async () => {
152152 const response = await fetch (` /api/users/${id } ` );
153153 return response .json ();
154154 })
@@ -185,8 +185,8 @@ const pollJobStatus = createAsyncPoller(
185185 return response .json ();
186186 },
187187 {
188- interval: 1000 , // Poll every second
189- maxAttempts: 30 , // Try up to 30 times
188+ interval: 1000 , // Poll every second
189+ maxAttempts: 30 , // Try up to 30 times
190190 backoff: {
191191 type: ' exponential' ,
192192 factor: 2 ,
@@ -196,7 +196,7 @@ const pollJobStatus = createAsyncPoller(
196196 shouldContinue : (result ) => result .status === ' running' ,
197197 onProgress : (result ) => {
198198 console .log (' Job progress:' , result .progress );
199- }
199+ },
200200 }
201201);
202202
0 commit comments