UNPKG

fluture-retry

Version:

Toolset for retrying potentially failing computations

100 lines (87 loc) 3.3 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('fluture')) : typeof define === 'function' && define.amd ? define(['exports', 'fluture'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.flutureRetry = {}, global.Fluture)); }(this, (function (exports, index_js) { 'use strict'; //. # Fluture retry // last :: NonEmpty (Array a) -> a function last(xs) { return xs[xs.length - 1]; } //. ## API //. //# retry :: (Number -> Number) -> Number -> Future a b -> Future (Array a) b //. //. Create a retrying Future using the given parameters: //. //. 1. A function over the amount of failures to determine waiting time. //. See [`exponentially`](#exponentially), [`linearly`](#linearly) and //. [`statically`](#statically) for pre-baked functions of this sort. //. 1. The maximum number of retries before failing. //. 1. A Future representing the computation to retry. //. //. See [Advanced usage](#advanced-usage) for an example. function retry(time) { return function retry(max) { return function retry(task) { var failures = new Array (max); return (function recur(i) { return task.pipe (index_js.chainRej (function(failure) { failures[i] = failure; var total = i + 1; return total === max ? index_js.reject (failures) : index_js.chain (recur) (index_js.after (time (total)) (total)); })); } (0)); }; }; } //# exponentially :: Number -> Number -> Number //. //. Takes two numbers and returns the result of multiplying the first by //. the second raised to the power of two. To be partially applied and used //. as a first argument to `retry`. function exponentially(t) { return function exponentially(n) { return t * Math.pow (n, 2); }; } //# linearly :: Number -> Number -> Number //. //. Takes two numbers and returns the result of multiplying them. To be //. partially applied and used as a first argument to `retry`. function linearly(t) { return function linearly(n) { return t * n; }; } //# statically :: a -> b -> a //. //. Takes two values and returns the first. To be partially applied and used //. as a first argument to `retry`. function statically(t) { return function statically(_) { return t; }; } //# linearSeconds :: Number -> Number //. //. Takes a number and multiplies it by 1000. var linearSeconds = linearly (1000); //# retryLinearly :: Future a b -> Future a b //. //. A pre-baked retry strategy. See [Basic usage](#basic-usage). function retryLinearly(task) { return index_js.mapRej (last) (retry (linearSeconds) (5) (task)); } //. [Fluture]: https://github.com/fluture-js/Fluture //. [esm]: https://github.com/standard-things/esm //. [UMD]: https://github.com/umdjs/umd exports.exponentially = exponentially; exports.linearSeconds = linearSeconds; exports.linearly = linearly; exports.retry = retry; exports.retryLinearly = retryLinearly; exports.statically = statically; Object.defineProperty(exports, '__esModule', { value: true }); })));