Last active
May 20, 2025 15:20
-
-
Save vitaly-t/6e3d285854d882b1618c7e435df164c4 to your computer and use it in GitHub Desktop.
Revisions
-
vitaly-t revised this gist
Apr 13, 2025 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -27,7 +27,7 @@ export type RetryStatus<D = unknown> = { /** * Retry-status callback type. */ export type RetryCB<T, D = unknown> = (s: RetryStatus<D>) => T; /** * Type for options passed into retryAsync function. -
vitaly-t revised this gist
Apr 6, 2025 . 1 changed file with 27 additions and 13 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,55 +1,69 @@ /** * Retry-status object type, for use with RetryCB. */ export type RetryStatus<D = unknown> = { /** * Retry index, starting from 0. */ readonly index: number, /** * Retry overall duration, in milliseconds. */ readonly duration: number, /** * Last error, if available; * it is undefined only when "retryAsync" calls "func" with index = 0. */ readonly error?: Error, /** * Extra data for status handlers, if specified. */ readonly data?: D }; /** * Retry-status callback type. */ export type RetryCB<T, D> = (s: RetryStatus<D>) => T; /** * Type for options passed into retryAsync function. */ export type RetryOptions<D = unknown> = { /** * Maximum number of retries (infinite by default), * or a callback to indicate the need for another retry. */ readonly retry?: number | RetryCB<boolean, D>, /** * Retry delays, in milliseconds (no delay by default), * or a callback that returns the delays. */ readonly delay?: number | RetryCB<number, D>, /** * Error notifications. */ readonly error?: RetryCB<void, D>, /** * Extra data for status handlers. */ readonly data?: D }; /** * Retries async operation returned from "func" callback, according to "options". */ export function retryAsync<T, D>(func: RetryCB<Promise<T>, D>, options?: RetryOptions<D>): Promise<T> { const start = Date.now(); let index = 0, e: any; let {retry = Number.POSITIVE_INFINITY, delay = -1, error, data} = options ?? {}; const s = () => ({index, duration: Date.now() - start, error: e, data}); const c = (): Promise<T> => func(s()).catch(err => { e = err; typeof error === 'function' && error(s()); @@ -61,4 +75,4 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(c) : c(); }); return c(); } -
vitaly-t revised this gist
Sep 7, 2024 . 1 changed file with 9 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -3,15 +3,15 @@ */ export type RetryStatus = { /** * Retry index, starting from 0. */ index: number, /** * Retry overall duration, in milliseconds. */ duration: number, /** * Last error, if available; * it is undefined only when "retryAsync" calls "func" with index = 0. */ error?: any @@ -27,16 +27,17 @@ export type RetryCB<T> = (s: RetryStatus) => T; */ export type RetryOptions = { /** * Maximum number of retries (infinite by default), * or a callback to indicate the need for another retry. */ retry?: number | RetryCB<boolean>, /** * Retry delays, in milliseconds (no delay by default), * or a callback that returns the delays. */ delay?: number | RetryCB<number>, /** * Error notifications. */ error?: RetryCB<void> }; @@ -60,4 +61,4 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(c) : c(); }); return c(); } -
vitaly-t revised this gist
Aug 31, 2024 . 1 changed file with 4 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -42,23 +42,22 @@ export type RetryOptions = { }; /** * Retries async operation returned from "func" callback, according to "options". */ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions): Promise<T> { const start = Date.now(); let index = 0, e: any; let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options ?? {}; const s = () => ({index, duration: Date.now() - start, error: e}); const c = (): Promise<T> => func(s()).catch(err => { e = err; typeof error === 'function' && error(s()); if ((typeof retry === 'function' ? (retry(s()) ? 1 : 0) : retry--) <= 0) { return Promise.reject(e); } const d = typeof delay === 'function' ? delay(s()) : delay; index++; return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(c) : c(); }); return c(); } -
vitaly-t revised this gist
Aug 30, 2024 . 1 changed file with 5 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -53,10 +53,12 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) e = err; typeof error === 'function' && error(s()); const r = typeof retry === 'function' ? (retry(s()) ? 1 : 0) : retry--; if (r <= 0) { return Promise.reject(e); } const d = typeof delay === 'function' ? delay(s()) : delay; index++; return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(c) : c(); }); return c(); } -
vitaly-t revised this gist
Aug 18, 2024 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -48,14 +48,14 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) const start = Date.now(); let index = 0, e: any; let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options ?? {}; const s = () => ({index, duration: Date.now() - start, error: e}); const c: () => Promise<T> = () => func(s()).catch(err => { e = err; typeof error === 'function' && error(s()); const r = typeof retry === 'function' ? (retry(s()) ? 1 : 0) : retry--; const d = typeof delay === 'function' ? delay(s()) : delay; index++; const t = () => r > 0 ? c() : Promise.reject(e); return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(t) : t(); }); return c(); -
vitaly-t revised this gist
Aug 18, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -44,7 +44,7 @@ export type RetryOptions = { /** * Retries async operation returned from "func" callback, according to options. */ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions): Promise<T> { const start = Date.now(); let index = 0, e: any; let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options ?? {}; -
vitaly-t revised this gist
Aug 14, 2024 . 1 changed file with 2 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -12,7 +12,6 @@ export type RetryStatus = { duration: number, /** * last error, if available; * it is undefined only when "retryAsync" calls "func" with index = 0. */ error?: any @@ -53,8 +52,8 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) const c: () => Promise<T> = () => func(s()).catch(err => { e = err; typeof error === 'function' && error(s()); // error notification const r = typeof retry === 'function' ? retry(s()) : retry--; // retry flag/value const d = typeof delay === 'function' ? delay(s()) : delay; // delay value index++; const t = () => r ? c() : Promise.reject(e); // retry vs reject test return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(t) : t(); -
vitaly-t revised this gist
Aug 14, 2024 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,9 @@ export type RetryStatus = { */ duration: number, /** * last error, if available; * * it is undefined only when "retryAsync" calls "func" with index = 0. */ error?: any }; @@ -42,8 +44,6 @@ export type RetryOptions = { /** * Retries async operation returned from "func" callback, according to options. */ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) { const start = Date.now(); -
vitaly-t revised this gist
Aug 14, 2024 . 1 changed file with 43 additions and 21 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,41 +1,63 @@ /** * Retry-status object type, for use with RetryCB. */ export type RetryStatus = { /** * retry index, starting from 0 */ index: number, /** * retry overall duration, in ms */ duration: number, /** * last error, if available */ error?: any }; /** * Retry-status callback type. */ export type RetryCB<T> = (s: RetryStatus) => T; /** * Type for options passed into retryAsync function. */ export type RetryOptions = { /** * maximum number of retries (infinite by default), * or a callback to indicate the need for another retry */ retry?: number | RetryCB<boolean>, /** * retry delays, in ms, or a callback that returns them */ delay?: number | RetryCB<number>, /** * error notifications */ error?: RetryCB<void> }; /** * Retries async operation returned from "func" callback, according to options. * * Note that "func()" will receive "error" = undefined when "index" = 0. */ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) { const start = Date.now(); let index = 0, e: any; let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options ?? {}; const s = () => ({index, duration: Date.now() - start, error: e}); // status creator const c: () => Promise<T> = () => func(s()).catch(err => { e = err; typeof error === 'function' && error(s()); // error notification const r = typeof retry === 'function' ? retry(s()) : retry--; // get retry flag const d = typeof delay === 'function' ? delay(s()) : delay; // get delay value index++; const t = () => r ? c() : Promise.reject(e); // retry vs reject test return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(t) : t(); }); return c(); } -
vitaly-t revised this gist
Aug 14, 2024 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -32,8 +32,8 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) const r = typeof retry === 'function' ? retry(s()) : retry--; const d = typeof delay === 'function' ? delay(s()) : delay; index++; const t = () => r ? c() : Promise.reject(e); return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(t) : t(); }); return c(); } -
vitaly-t revised this gist
Aug 11, 2024 . 1 changed file with 5 additions and 6 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,23 +1,22 @@ // retry-status object: // - "index": retry index, starting from 0 // - "duration": retry overall duration, in ms // - "error": last error, if available export type RetryStatus = { index: number, duration: number, error?: any }; // retry-status callback; export type RetryCB<T> = (s: RetryStatus) => T; export type RetryOptions = { // maximum number of retries (infinite by default), // or a callback to indicate the need for another retry; retry?: number | RetryCB<boolean>, // retry delays, in ms, or a callback that returns them; delay?: number | RetryCB<number>, // error notifications; error?: RetryCB<void> }; // retries async operation returned from "func" callback, according to options; @@ -29,7 +28,7 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) const s = () => ({index, duration: Date.now() - start, error: e}); const c: () => Promise<T> = () => func(s()).catch(err => { e = err; typeof error === 'function' && error(s()); const r = typeof retry === 'function' ? retry(s()) : retry--; const d = typeof delay === 'function' ? delay(s()) : delay; index++; -
vitaly-t revised this gist
Aug 11, 2024 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,3 +38,5 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) }); return c(); } // Tested against TypeScript 5.x, in strict mode. -
vitaly-t revised this gist
Aug 11, 2024 . 1 changed file with 3 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,10 @@ // retry status object: // - "index": retry index, starting from 0 // - "duration": retry overall duration, in ms // - "error": last error, if available export type RetryStatus = { index: number, duration: number, error?: any }; // retry status for callbacks; export type RetryCB<T> = (s: RetryStatus) => T; export type RetryOptions = { @@ -26,7 +26,7 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) const start = Date.now(); let index = 0, e: any; let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options ?? {}; const s = () => ({index, duration: Date.now() - start, error: e}); const c: () => Promise<T> = () => func(s()).catch(err => { e = err; typeof error === 'function' && error(e, index + 1); @@ -38,9 +38,3 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) }); return c(); } -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,5 +41,6 @@ export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) /** TIPS: - If you need a timeout, use callback for "retry", checking for "duration" in the status; though it is a between-retry timeout, i.e. we do not cancel promises here ;) */ -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 9 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,12 +2,12 @@ // - "index": retry index, starting from 0 // - "duration": retry overall duration, in ms // - "error": last retry error, if available export type RetryStatus = { index: number, duration: number, error?: any }; // retry status callback; export type RetryCB<T> = (s: RetryStatus) => T; export type RetryOptions = { // maximum number of retries (infinite by default), // or a callback to indicate the need for another retry; retry?: number | RetryCB<boolean>, @@ -22,7 +22,7 @@ type RetryOptions = { // retries async operation returned from "func" callback, according to options; // note that "func()" will receive "error" = undefined when "index" = 0. export function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) { const start = Date.now(); let index = 0, e: any; let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options ?? {}; @@ -38,3 +38,8 @@ function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) { }); return c(); } /** TIPS: - If you need a timeout, use callback for "retry", checking for "duration" in the status. */ -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,7 +5,7 @@ type RetryStatus = { index: number, duration: number, error?: any }; // retry status callback; type RetryCB<T> = (s: RetryStatus) => T; type RetryOptions = { // maximum number of retries (infinite by default), -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 17 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,11 @@ // retry status object: // - "index": retry index, starting from 0 // - "duration": retry overall duration, in ms // - "error": last retry error, if available type RetryStatus = { index: number, duration: number, error?: any }; // retry status callback; type RetryCB<T> = (cb: RetryStatus) => T; type RetryOptions = { // maximum number of retries (infinite by default), @@ -16,16 +21,18 @@ type RetryOptions = { }; // retries async operation returned from "func" callback, according to options; // note that "func()" will receive "error" = undefined when "index" = 0. function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) { const start = Date.now(); let index = 0, e: any; let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options ?? {}; const s = () => ({index, duration: Date.now() - start, error: e}); // status const c: () => Promise<T> = () => func(s()).catch(err => { e = err; typeof error === 'function' && error(e, index + 1); const r = typeof retry === 'function' ? retry(s()) : retry--; const d = typeof delay === 'function' ? delay(s()) : delay; index++; const p = () => r ? c() : Promise.reject(e); return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(p) : p(); }); -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -18,7 +18,7 @@ type RetryOptions = { // retries async operation returned from "func" callback, according to options; // note that "func(i, e)" will receive "e" = undefined when "i" = 0. function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) { let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options ?? {}; let i = 0, e: any; const c: () => Promise<T> = () => func(i, e).catch(err => { e = err; -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 23 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,24 +1,33 @@ // retry status callback, which receives 2 parameters: // retry index (>=0), plus the last error (if available) type RetryCB<T> = (i: number, e: any) => T; type RetryOptions = { // maximum number of retries (infinite by default), // or a callback to indicate the need for another retry; retry?: number | RetryCB<boolean>, // retry delays, in ms, or a callback that returns them; delay?: number | RetryCB<number>, // error notifications; // it receives: the error + errors count (>=1). error?: (e: any, c: number) => void }; // retries async operation returned from "func" callback, according to options; // note that "func(i, e)" will receive "e" = undefined when "i" = 0. function retryAsync<T>(func: RetryCB<Promise<T>>, options?: RetryOptions) { let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options || {}; let i = 0, e: any; const c: () => Promise<T> = () => func(i, e).catch(err => { e = err; typeof error === 'function' && error(e, i + 1); const r = typeof retry === 'function' ? retry(i, e) : retry--; const d = typeof delay === 'function' ? delay(i, e) : delay; i++; const p = () => r ? c() : Promise.reject(e); return d >= 0 ? (new Promise(a => setTimeout(a, d))).then(p) : p(); }); return c(); } -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,21 +1,21 @@ type AsyncRetryOptions = { // maximum number of retries (infinite by default); retry?: number, // retry delays, in ms, or a callback that returns them; // the callback receives: retry index (from 0) + the error. delay?: number | ((i: number, e: any) => number), // error notifications; error?: (e: any) => void }; // retries async operation returned from "func" callback, according to options function retryAsync<T>(func: () => Promise<T>, options?: AsyncRetryOptions) { let {retry = Number.POSITIVE_INFINITY, delay = -1, error} = options || {}; let i = 0; // retry index const c: () => Promise<T> = () => func().catch(e => { const r = () => retry-- ? c() : Promise.reject(e); typeof error === 'function' && error(e); const d = typeof delay === 'function' ? delay(i++, e) : delay; return d >= 0 ? (new Promise(r => setTimeout(r, d))).then(r) : r(); -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ type AsyncRetryOptions = { // the callback receives: retry index (from 0) + the error. delay?: number | ((i: number, e: any) => number), // errors notification; error?: (e: any) => void }; -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -13,7 +13,7 @@ type AsyncRetryOptions = { // retries async operation returned from "func" callback, according to options function retryAsync<T>(func: () => Promise<T>, options?: AsyncRetryOptions) { let {retries = Number.POSITIVE_INFINITY, delay = -1, error} = options || {}; let i = 0; // retry index const c: () => Promise<T> = () => func().catch(e => { const r = () => retries-- ? c() : Promise.reject(e); typeof error === 'function' && error(e); -
vitaly-t revised this gist
Aug 10, 2024 . 1 changed file with 16 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,24 @@ type AsyncRetryOptions = { // maximum number of retries (infinite by default); retries?: number, // retry delays, in ms, or a callback that returns one; // the callback receives: retry index (from 0) + the error. delay?: number | ((i: number, e: any) => number), // errors notification: error?: (e: any) => void }; // retries async operation returned from "func" callback, according to options function retryAsync<T>(func: () => Promise<T>, options?: AsyncRetryOptions) { let {retries = Number.POSITIVE_INFINITY, delay = -1, error} = options || {}; let i = 0;// retry index const c: () => Promise<T> = () => func().catch(e => { const r = () => retries-- ? c() : Promise.reject(e); typeof error === 'function' && error(e); const d = typeof delay === 'function' ? delay(i++, e) : delay; return d >= 0 ? (new Promise(r => setTimeout(r, d))).then(r) : r(); }); return c(); } -
vitaly-t created this gist
Aug 9, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,16 @@ type AsyncRetryOptions = { retries?: number, // maximum retries (infinite by default) delay?: number, // delay between retries, in ms error?: (e: any) => void // errors notification }; function retryAsync<T>(func: () => Promise<T>, options?: AsyncRetryOptions) { let {retries = Number.POSITIVE_INFINITY, delay = -1, error} = options || {}; const call: () => Promise<T> = () => func().catch(e => { const r = () => retries-- ? call() : Promise.reject(e); typeof error === 'function' && error(e); return delay >= 0 ? (new Promise(r => setTimeout(r, delay))).then(r) : r(); }); return call(); }