JSPM

  • Created
  • Published
  • Downloads 49456
  • Score
    100M100P100Q142872F
  • License MIT

Package Exports

  • ts-retry

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (ts-retry) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

ts-retry

A little retry tool in javascript/typescript for node and for browser. Can also bind a timeout to a function.Ex:

const result = await retryAsync(
  async ()=> {/* get some data if ready, throw an expection otherwise */}, 
  {delay:100,maxTry:5}
)

This will try 5 times to get the data. If data is not ready after the 5 attempts, an exception is thrown. If data are obtained, retryAsync stop immediatly and returns the data.

How to:

  • to retry something:
    const result = await retry(()=> {/* do something */}, {delay:100,maxTry:5})
  • to retry something async :
    const result = await retryAsync(async ()=> {/* do something */}, {delay:100,maxTry:5})

Above examples make up to 5 attempts, waiting 100ms between each try.

  • to set a timeout:
    try {
      const result = await waitUntil(async ()=> {/* do something */}, 10000);
    } catch (err) {
      if (error instanceof TimeoutError) {
        // fn does not complete
      } else {
        // fn throws an exception
      }
    }
    • to set a timeout on something async :
    try {
      const result = await waitUntilAsync(async ()=> {/* do something */}, 10000);
    } catch (err) {
      if (error instanceof TimeoutError) {
        // fn does not complete
      } else {
        // fn throws an exception
      }
    }

Above examples fn has 10 seconds to complete, otherwhise an exception is thrown.

API

  • retry(fn, retryOptions): call repeteadly fn until fn does not throw and exception. Stop after retryOptions.maxTry count. Between each call wait retryOptions.delay milliseconds. if stop to call fn after retryOptions.maxTry, throws fn execption, otherwise returns fn return value.
  • retryAsync(fn, retryOptions): same as retry, except fn is an asynchronous function.
  • retryOptions:
    • maxTry maximum calls to fn.
    • delay: delay between each call (in milliseconds).
  • waitUntil(fn, delay, error?): waitUntil call asynchronously fn once. If fn complete within the delay (express in miliseconds), waitUntil returns the fn result. Otherwhise it thows the given error (if any) or a TimeoutError exception.
  • waitUntilAsync(fn, delay, error?): same as waitUntil, except fn is an asynchronous function.
  • TimeoutError: an error thrown by waitUntil and waitUntilAsync. It has a property isTimeout set to true: therefore there's two means to check os fn timeout:
  error instanceof TimeoutError
  or
  (error as any).isTimeout

In case of timeout fn is still executing. It is advise to add a mean to abort it. Note: retry, retryAsync and waitUntil return type is the return type of the given fn.

Typescript:

The library comes with it's own .d.ts file.

Each function return type is the return type of the callback

  const get = (); boolean => /* do something that return a boolean */
  const result = await retryAsync>(get, {delay:100,maxTry:5}) /* result is a boolean */

Compatilibity

This libaray is provided in ESM and commomJS (see /lib). This lib works with Deno (to import it,use the url https://raw.githubusercontent.com/franckLdx/ts-retry/<version>/src/index.ts). However it's more convenient to use the specific port of this lib to Deno: see https://deno.land/x/retry