promise-assist
Advanced tools
Comparing version 1.1.1 to 1.2.0
export interface IRetryOptions { | ||
/** | ||
* Number of times to retry action if it rejects. | ||
* Pass -1 for infinite retries. | ||
* Pass `Infinity` for infinite retries. | ||
* | ||
@@ -6,0 +6,0 @@ * @default 3 |
@@ -65,5 +65,5 @@ "use strict"; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, retries, delay, timeout, timedOut, timeoutId, timeoutPromise, maxAttempts, attemptCount, lastError, result, e_1; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
var _a, retries, delay, timeout, lastError, timedOut, timeoutId, timeoutPromise, maxAttempts, attemptCount, actionResult, result, e_1, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
@@ -76,30 +76,48 @@ _a = __assign({}, defaultOptions, options), retries = _a.retries, delay = _a.delay, timeout = _a.timeout; | ||
timedOut = true; | ||
rej(new Error("timed out after " + timeout + "ms")); | ||
if (!lastError) { | ||
lastError = new Error("timed out after " + timeout + "ms"); | ||
} | ||
rej(); | ||
}, timeout); | ||
} | ||
}); | ||
maxAttempts = retries === -1 ? Infinity : retries + 1; | ||
maxAttempts = retries + 1; | ||
attemptCount = 0; | ||
_b.label = 1; | ||
_c.label = 1; | ||
case 1: | ||
attemptCount++; | ||
_b.label = 2; | ||
_c.label = 2; | ||
case 2: | ||
_b.trys.push([2, 4, , 6]); | ||
return [4 /*yield*/, Promise.race([action(), timeoutPromise])]; | ||
_c.trys.push([2, 4, , 5]); | ||
actionResult = action(); | ||
if (actionResult instanceof Promise) { | ||
// make sure we always save error of original promise | ||
// Promise.race below might loose it due to timeout | ||
actionResult.catch(function (e) { return lastError = e || lastError; }); | ||
} | ||
return [4 /*yield*/, Promise.race([actionResult, timeoutPromise])]; | ||
case 3: | ||
result = _b.sent(); | ||
result = _c.sent(); | ||
clearTimeout(timeoutId); | ||
return [2 /*return*/, result]; | ||
case 4: | ||
e_1 = _b.sent(); | ||
lastError = e_1; | ||
return [4 /*yield*/, Promise.race([sleep_1.sleep(delay), timeoutPromise])]; | ||
e_1 = _c.sent(); | ||
lastError = e_1 || lastError; | ||
return [3 /*break*/, 5]; | ||
case 5: | ||
_b.sent(); | ||
return [3 /*break*/, 6]; | ||
if (!(delay > 0)) return [3 /*break*/, 9]; | ||
_c.label = 6; | ||
case 6: | ||
_c.trys.push([6, 8, , 9]); | ||
return [4 /*yield*/, Promise.race([sleep_1.sleep(delay), timeoutPromise])]; | ||
case 7: | ||
_c.sent(); | ||
return [3 /*break*/, 9]; | ||
case 8: | ||
_b = _c.sent(); | ||
return [3 /*break*/, 9]; | ||
case 9: | ||
if (!timedOut && (attemptCount < maxAttempts)) return [3 /*break*/, 1]; | ||
_b.label = 7; | ||
case 7: | ||
_c.label = 10; | ||
case 10: | ||
clearTimeout(timeoutId); | ||
@@ -113,5 +131,5 @@ throw (lastError || new Error("failed after " + attemptCount + " tries")); | ||
function waitFor(action, options) { | ||
return retry(action, __assign({ delay: 10, timeout: 500, retries: -1 }, options)); | ||
return retry(action, __assign({ delay: 10, timeout: 500, retries: Infinity }, options)); | ||
} | ||
exports.waitFor = waitFor; | ||
//# sourceMappingURL=retry.js.map |
export interface IRetryOptions { | ||
/** | ||
* Number of times to retry action if it rejects. | ||
* Pass -1 for infinite retries. | ||
* Pass `Infinity` for infinite retries. | ||
* | ||
@@ -6,0 +6,0 @@ * @default 3 |
@@ -63,5 +63,5 @@ var __assign = (this && this.__assign) || function () { | ||
return __awaiter(this, void 0, void 0, function () { | ||
var _a, retries, delay, timeout, timedOut, timeoutId, timeoutPromise, maxAttempts, attemptCount, lastError, result, e_1; | ||
return __generator(this, function (_b) { | ||
switch (_b.label) { | ||
var _a, retries, delay, timeout, lastError, timedOut, timeoutId, timeoutPromise, maxAttempts, attemptCount, actionResult, result, e_1, _b; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
@@ -74,30 +74,48 @@ _a = __assign({}, defaultOptions, options), retries = _a.retries, delay = _a.delay, timeout = _a.timeout; | ||
timedOut = true; | ||
rej(new Error("timed out after " + timeout + "ms")); | ||
if (!lastError) { | ||
lastError = new Error("timed out after " + timeout + "ms"); | ||
} | ||
rej(); | ||
}, timeout); | ||
} | ||
}); | ||
maxAttempts = retries === -1 ? Infinity : retries + 1; | ||
maxAttempts = retries + 1; | ||
attemptCount = 0; | ||
_b.label = 1; | ||
_c.label = 1; | ||
case 1: | ||
attemptCount++; | ||
_b.label = 2; | ||
_c.label = 2; | ||
case 2: | ||
_b.trys.push([2, 4, , 6]); | ||
return [4 /*yield*/, Promise.race([action(), timeoutPromise])]; | ||
_c.trys.push([2, 4, , 5]); | ||
actionResult = action(); | ||
if (actionResult instanceof Promise) { | ||
// make sure we always save error of original promise | ||
// Promise.race below might loose it due to timeout | ||
actionResult.catch(function (e) { return lastError = e || lastError; }); | ||
} | ||
return [4 /*yield*/, Promise.race([actionResult, timeoutPromise])]; | ||
case 3: | ||
result = _b.sent(); | ||
result = _c.sent(); | ||
clearTimeout(timeoutId); | ||
return [2 /*return*/, result]; | ||
case 4: | ||
e_1 = _b.sent(); | ||
lastError = e_1; | ||
return [4 /*yield*/, Promise.race([sleep(delay), timeoutPromise])]; | ||
e_1 = _c.sent(); | ||
lastError = e_1 || lastError; | ||
return [3 /*break*/, 5]; | ||
case 5: | ||
_b.sent(); | ||
return [3 /*break*/, 6]; | ||
if (!(delay > 0)) return [3 /*break*/, 9]; | ||
_c.label = 6; | ||
case 6: | ||
_c.trys.push([6, 8, , 9]); | ||
return [4 /*yield*/, Promise.race([sleep(delay), timeoutPromise])]; | ||
case 7: | ||
_c.sent(); | ||
return [3 /*break*/, 9]; | ||
case 8: | ||
_b = _c.sent(); | ||
return [3 /*break*/, 9]; | ||
case 9: | ||
if (!timedOut && (attemptCount < maxAttempts)) return [3 /*break*/, 1]; | ||
_b.label = 7; | ||
case 7: | ||
_c.label = 10; | ||
case 10: | ||
clearTimeout(timeoutId); | ||
@@ -110,4 +128,4 @@ throw (lastError || new Error("failed after " + attemptCount + " tries")); | ||
export function waitFor(action, options) { | ||
return retry(action, __assign({ delay: 10, timeout: 500, retries: -1 }, options)); | ||
return retry(action, __assign({ delay: 10, timeout: 500, retries: Infinity }, options)); | ||
} | ||
//# sourceMappingURL=retry.js.map |
{ | ||
"name": "promise-assist", | ||
"description": "Several helper functions when working with native promises", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"main": "./cjs/index.js", | ||
@@ -6,0 +6,0 @@ "module": "./esm/index.js", |
@@ -74,2 +74,3 @@ # promise-assist | ||
// with default options | ||
retry(() => fetch('http://some-url/asset.json')) | ||
@@ -80,6 +81,7 @@ .then(value => value.json()) | ||
// with custom options | ||
retry(() => fetch('http://some-url/asset.json'), { | ||
retries: -1, // infinite number of retries | ||
delay: 10 * 1000, // 10 seconds delay between retries | ||
timeout: 2 * 60 * 1000 // 2 minutes timeout to stop trying | ||
retries: Infinity, // infinite number of retries | ||
delay: 10 * 1000, // 10 seconds delay between retries | ||
timeout: 2 * 60 * 1000 // 2 minutes timeout to stop trying | ||
}) | ||
@@ -86,0 +88,0 @@ .then(value => value.json()) |
@@ -6,3 +6,3 @@ import { sleep } from './sleep' | ||
* Number of times to retry action if it rejects. | ||
* Pass -1 for infinite retries. | ||
* Pass `Infinity` for infinite retries. | ||
* | ||
@@ -48,2 +48,3 @@ * @default 3 | ||
let lastError: Error | undefined // we expose last error if all attempts failed | ||
let timedOut = false | ||
@@ -56,3 +57,6 @@ let timeoutId: number | undefined // so we can cancel the timeout rejection | ||
timedOut = true | ||
rej(new Error(`timed out after ${timeout}ms`)) | ||
if (!lastError) { | ||
lastError = new Error(`timed out after ${timeout}ms`) | ||
} | ||
rej() | ||
}, timeout) | ||
@@ -62,5 +66,4 @@ } | ||
const maxAttempts = retries === -1 ? Infinity : retries + 1 | ||
const maxAttempts = retries + 1 | ||
let attemptCount = 0 | ||
let lastError: Error | ||
@@ -70,8 +73,16 @@ do { | ||
try { | ||
const result = await Promise.race([action(), timeoutPromise]) | ||
const actionResult = action() | ||
if (actionResult instanceof Promise) { | ||
// make sure we always save error of original promise | ||
// Promise.race below might loose it due to timeout | ||
actionResult.catch(e => lastError = e || lastError) | ||
} | ||
const result = await Promise.race([actionResult, timeoutPromise]) | ||
clearTimeout(timeoutId) | ||
return result | ||
} catch (e) { | ||
lastError = e | ||
await Promise.race([sleep(delay), timeoutPromise]) | ||
} catch (e) { lastError = e || lastError } | ||
if (delay > 0) { | ||
try { | ||
await Promise.race([sleep(delay), timeoutPromise]) | ||
} catch { /* we throw lastError at the end */ } | ||
} | ||
@@ -88,3 +99,3 @@ } while (!timedOut && (attemptCount < maxAttempts)) | ||
): Promise<T> { | ||
return retry(action, { delay: 10, timeout: 500, retries: -1, ...options }) | ||
return retry(action, { delay: 10, timeout: 500, retries: Infinity, ...options }) | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
41620
663
94