@humanwhocodes/retry
Advanced tools
Comparing version 0.1.2 to 0.1.3
/** | ||
* @fileoverview A utility for retrying failed async method calls. | ||
*/ | ||
/* global setTimeout, clearTimeout */ | ||
//----------------------------------------------------------------------------- | ||
@@ -187,3 +190,4 @@ // Constants | ||
} catch (error) { | ||
return Promise.reject(new Error("Cannot catch synchronous errors.")); | ||
// @ts-ignore TS doesn't know about the second argument to Error? | ||
return Promise.reject(new Error(`Synchronous error: ${error.message}`, { cause: error })); | ||
} | ||
@@ -190,0 +194,0 @@ |
@@ -1,1 +0,1 @@ | ||
class RetryTask{fn;error;timestamp=Date.now();lastAttempt=this.timestamp;resolve;reject;constructor(t,e,r,s){this.fn=t,this.error=e,this.timestamp=Date.now(),this.lastAttempt=Date.now(),this.resolve=r,this.reject=s}}class Retrier{#t=[];#e;#r;#s;#i;constructor(t,{timeout:e=6e4,maxDelay:r=100}={}){if("function"!=typeof t)throw new Error("Missing function to check errors");this.#i=t,this.#e=e,this.#r=r}retry(t){let e;try{e=t()}catch(t){return Promise.reject(new Error("Cannot catch synchronous errors."))}return e instanceof Promise?e.catch((e=>{if(!this.#i(e))throw e;return new Promise(((r,s)=>{this.#t.push(new RetryTask(t,e,r,s)),this.#o()}))})):Promise.reject(new Error("Result is not a promise."))}#o(){clearTimeout(this.#s),this.#s=void 0;const t=this.#t.shift();if(t){if(function(t,e){return Date.now()-t.timestamp>e}(t,this.#e))return t.reject(t.error),void this.#o();if(!function(t,e){const r=Date.now()-t.lastAttempt,s=Math.max(t.lastAttempt-t.timestamp,1);return r>=Math.min(1.2*s,e)}(t,this.#r))return this.#t.unshift(t),void(this.#s=setTimeout((()=>this.#o()),0));t.lastAttempt=Date.now(),t.fn().then((e=>t.resolve(e))).catch((e=>{this.#i(e)?(t.lastAttempt=Date.now(),this.#t.push(t)):t.reject(e)})).finally((()=>this.#o()))}}}export{Retrier}; | ||
class RetryTask{fn;error;timestamp=Date.now();lastAttempt=this.timestamp;resolve;reject;constructor(t,e,r,s){this.fn=t,this.error=e,this.timestamp=Date.now(),this.lastAttempt=Date.now(),this.resolve=r,this.reject=s}}class Retrier{#t=[];#e;#r;#s;#i;constructor(t,{timeout:e=6e4,maxDelay:r=100}={}){if("function"!=typeof t)throw new Error("Missing function to check errors");this.#i=t,this.#e=e,this.#r=r}retry(t){let e;try{e=t()}catch(t){return Promise.reject(new Error(`Synchronous error: ${t.message}`,{cause:t}))}return e instanceof Promise?e.catch((e=>{if(!this.#i(e))throw e;return new Promise(((r,s)=>{this.#t.push(new RetryTask(t,e,r,s)),this.#o()}))})):Promise.reject(new Error("Result is not a promise."))}#o(){clearTimeout(this.#s),this.#s=void 0;const t=this.#t.shift();if(t){if(function(t,e){return Date.now()-t.timestamp>e}(t,this.#e))return t.reject(t.error),void this.#o();if(!function(t,e){const r=Date.now()-t.lastAttempt,s=Math.max(t.lastAttempt-t.timestamp,1);return r>=Math.min(1.2*s,e)}(t,this.#r))return this.#t.unshift(t),void(this.#s=setTimeout((()=>this.#o()),0));t.lastAttempt=Date.now(),t.fn().then((e=>t.resolve(e))).catch((e=>{this.#i(e)?(t.lastAttempt=Date.now(),this.#t.push(t)):t.reject(e)})).finally((()=>this.#o()))}}}export{Retrier}; |
{ | ||
"name": "@humanwhocodes/retry", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"description": "A utility to retry failed async methods.", | ||
@@ -66,10 +66,10 @@ "type": "module", | ||
"@types/mocha": "^10.0.3", | ||
"@types/node": "20.8.9", | ||
"@types/node": "20.11.14", | ||
"eslint": "^8.21.0", | ||
"lint-staged": "15.2.0", | ||
"mocha": "10.2.0", | ||
"lint-staged": "15.2.1", | ||
"mocha": "^10.3.0", | ||
"rollup": "3.29.4", | ||
"typescript": "5.2.2", | ||
"typescript": "5.3.3", | ||
"yorkie": "2.0.0" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
40189
699