kuyoonjo / ctry

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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);
  }
})();

About

License:MIT License


Languages

Language:TypeScript 100.0%