# ctry

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

Latest version **1.1.1** (published 2024-11-20) · MIT license · 0 weekly downloads

## Install

```sh
npm install ctry
pnpm add ctry
yarn add ctry
bun add ctry
```

## Health

**Score 35/100 (D)** — status: maintenance-mode.

Positive: has types; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: stale; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.1 |
| Published | 2024-11-20 |
| First published | 2018-11-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | CommonJS |
| Node | >=6.0.0 |
| Dependencies | 0 |
| Unpacked size | 21.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Yu Chen |
| Maintainers | kuyoonjo |

## Links

- npm: https://www.npmjs.com/package/ctry
- Repository: https://github.com/kuyoonjo/ctry
- Homepage: https://github.com/kuyoonjo/ctry#readme
- Issues: https://github.com/kuyoonjo/ctry/issues
- npm.io page: https://npm.io/package/ctry

## Recent versions

- 1.1.1 (latest) — 2024-11-20
- 1.1.0 — 2024-11-20
- 1.0.1 — 2018-12-14
- 1.0.0 — 2018-11-12

## README

[![Build Status](https://travis-ci.org/kuyoonjo/ctry.svg?branch=master)](https://travis-ci.org/kuyoonjo/ctry.svg?branch=master)
[![Coverage Status](https://coveralls.io/repos/github/kuyoonjo/ctry/badge.svg?branch=master)](https://coveralls.io/github/kuyoonjo/ctry?branch=master)
[![MIT license](http://img.shields.io/badge/license-MIT-brightgreen.svg)](http://opensource.org/licenses/MIT)

# Intro

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

```ts
/**
 * 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.

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

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

---
_Source: https://npm.io/package/ctry · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
