1.0.1 • Published 5 years ago

ctry v1.0.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Build Status Coverage Status MIT license

Intro

ctry tries to resolve a promise with timeout and chance configurations.

/**
 * ctry
 * @param ctryFunc a function returns a promise
 * @param timeout timeout in ms
 * @param tries number of changes
 */
function ctry<T>(
  ctryFunc: CtryFunction<T>,
  timeout: number = 5000,
  tries: number = 1,
): Promise<T>;

Q: When shoul you use it? A: Trying to do sth. with timeout, and/or give it more than one chance.

eg. Trying validate a web server whether it is reachable. The codes use HEAD method to do a request, and try 3 times.

import axios from 'axios';
import { ctry } from 'ctry';

(async () => {
  try {
    await ctry(() => axios.head('https://example.com'), 5000, 3);
  } catch (e) {
    console.error(e);
  }
})();