@humanwhocodes/retry
Advanced tools
Comparing version 0.2.3 to 0.2.4
@@ -194,3 +194,3 @@ /** | ||
// if the result is not a promise then reject an error | ||
if (!(result instanceof Promise)) { | ||
if (!result || typeof result.then !== "function") { | ||
return Promise.reject(new Error("Result is not a promise.")); | ||
@@ -201,3 +201,3 @@ } | ||
// @ts-ignore because we know it's any | ||
return result.catch(error => { | ||
return Promise.resolve(result).catch(error => { | ||
if (!this.#check(error)) { | ||
@@ -245,3 +245,5 @@ throw error; | ||
task.lastAttempt = Date.now(); | ||
task.fn() | ||
// Promise.resolve needed in case it's a thenable but not a Promise | ||
Promise.resolve(task.fn()) | ||
// @ts-ignore because we know it's any | ||
@@ -248,0 +250,0 @@ .then(result => task.resolve(result)) |
@@ -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(`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}; | ||
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&&"function"==typeof e.then?Promise.resolve(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(),Promise.resolve(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.2.3", | ||
"version": "0.2.4", | ||
"description": "A utility to retry failed async methods.", | ||
@@ -5,0 +5,0 @@ "type": "module", |
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
40551
699
12
1
2
12