@appolo/utils
Advanced tools
Comparing version 8.0.8 to 8.0.9
@@ -15,1 +15,10 @@ export interface PromiseFulfilledResult<T> { | ||
export type IterateFunction<T, R> = (item: T, index: number | string) => Resolvable<R>; | ||
export interface IRetry { | ||
retires: number, | ||
max?: number, | ||
random?: number, | ||
min?: number, | ||
linear?: number, | ||
exponential?: number | ||
} |
@@ -8,2 +8,4 @@ "use strict"; | ||
const promiseSome_1 = require("./promiseSome"); | ||
const time_1 = require("../time"); | ||
const promiseCreate_1 = require("./promiseCreate"); | ||
class Promises { | ||
@@ -94,3 +96,3 @@ static delay(delay) { | ||
} | ||
static async retry(fn, retires = 1) { | ||
static async retry(fn, options = 1, retryCount = 0) { | ||
let [err, result] = await Promises.to(fn()); | ||
@@ -100,6 +102,14 @@ if (err == null) { | ||
} | ||
if (retires <= 0) { | ||
if (typeof options == "number") { | ||
options = { retires: options }; | ||
} | ||
retryCount++; | ||
if (retryCount > options.retires) { | ||
throw err; | ||
} | ||
return Promises.retry(fn, --retires); | ||
let delay = time_1.Time.calcBackOff(retryCount, options); | ||
if (delay) { | ||
await Promises.delay(delay); | ||
} | ||
return Promises.retry(fn, options, retryCount); | ||
} | ||
@@ -115,4 +125,7 @@ static promiseTimeout(promise, timeout) { | ||
} | ||
static create(fn) { | ||
return new promiseCreate_1.PromiseCreate(fn); | ||
} | ||
} | ||
exports.Promises = Promises; | ||
//# sourceMappingURL=promises.js.map |
@@ -5,3 +5,5 @@ import {Deferred} from "./deferred"; | ||
import {PromiseSome} from "./promiseSome"; | ||
import {IterateFunction, Resolvable} from "./interfaces"; | ||
import {IRetry, IterateFunction, Resolvable} from "./interfaces"; | ||
import {Time} from "../time"; | ||
import {PromiseCreate} from "./promiseCreate"; | ||
@@ -55,3 +57,3 @@ export class Promises { | ||
public static async to<T, K>(promise: Promise<T>): Promise<[K, T?]> { | ||
public static async to<T, K = any>(promise: Promise<T>): Promise<[K, T?]> { | ||
@@ -66,2 +68,3 @@ try { | ||
public static allSettled<T>(promises: Promise<T>[]): Promise<({ status: "fulfilled"; value: T; } | { status: "rejected"; reason: any; })[]> { | ||
@@ -124,3 +127,3 @@ | ||
public static async retry<T>(fn: () => Promise<T>, retires: number = 1): Promise<T> { | ||
public static async retry<T>(fn: () => Promise<T>, options: (number | IRetry) = 1, retryCount: number = 0): Promise<T> { | ||
let [err, result] = await Promises.to(fn()); | ||
@@ -132,7 +135,19 @@ | ||
if (retires <= 0) { | ||
if (typeof options == "number") { | ||
options = {retires: options} | ||
} | ||
retryCount++; | ||
if (retryCount > options.retires) { | ||
throw err; | ||
} | ||
return Promises.retry(fn, --retires); | ||
let delay = Time.calcBackOff(retryCount, options); | ||
if (delay) { | ||
await Promises.delay(delay); | ||
} | ||
return Promises.retry(fn, options, retryCount); | ||
} | ||
@@ -149,7 +164,6 @@ | ||
} | ||
public static create<T>(fn: () => Promise<T>): PromiseCreate<T> { | ||
return new PromiseCreate<T>(fn) | ||
} | ||
} | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Time = void 0; | ||
const index_1 = require("../index"); | ||
class Time { | ||
@@ -43,2 +44,19 @@ static currentTimeInterval(interval) { | ||
} | ||
static calcBackOff(retry, params) { | ||
let delay = 0; | ||
if (params.linear) { | ||
delay += params.linear * retry; | ||
} | ||
if (params.exponential) { | ||
delay += Math.pow(params.exponential, retry); | ||
} | ||
if (params.random) { | ||
delay += index_1.Numbers.random(0, params.random); | ||
} | ||
delay += (params.min || 0); | ||
if (params.max) { | ||
delay = Math.min(params.max, delay); | ||
} | ||
return Math.fround(delay); | ||
} | ||
} | ||
@@ -45,0 +63,0 @@ exports.Time = Time; |
@@ -0,1 +1,4 @@ | ||
import {Numbers} from "../index"; | ||
import {IRetry} from "./promises/interfaces"; | ||
export class Time { | ||
@@ -56,3 +59,27 @@ | ||
public static calcBackOff(retry: number, params: IRetry): number { | ||
let delay = 0; | ||
if (params.linear) { | ||
delay += params.linear * retry; | ||
} | ||
if (params.exponential) { | ||
delay += Math.pow(params.exponential, retry); | ||
} | ||
if (params.random) { | ||
delay += Numbers.random(0, params.random) | ||
} | ||
delay +=(params.min || 0); | ||
if (params.max) { | ||
delay = Math.min(params.max, delay) | ||
} | ||
return Math.fround(delay); | ||
} | ||
} |
@@ -20,3 +20,3 @@ { | ||
"main": "./index.js", | ||
"version": "8.0.8", | ||
"version": "8.0.9", | ||
"license": "MIT", | ||
@@ -23,0 +23,0 @@ "repository": { |
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
178830
89
3207