# promise-timeout-jz

> A setTimeout, but with promises.

Latest version **2.0.0** (published 2022-11-04) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-timeout-jz
pnpm add promise-timeout-jz
yarn add promise-timeout-jz
bun add promise-timeout-jz
```

## Health

**Score 20/100 (F)** — status: abandoned.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.0 |
| Published | 2022-11-04 |
| First published | 2022-10-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 7.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | OWLjz18 |
| Maintainers | owljz18 |
| Keywords | timeout, setTimeout, promises, timeout-jz |

## Links

- npm: https://www.npmjs.com/package/promise-timeout-jz
- npm.io page: https://npm.io/package/promise-timeout-jz

## Alternatives

- [@commercetools/sync-actions](https://npm.io/package/@commercetools/sync-actions.md) — 25.1K weekly downloads
- [cwait](https://npm.io/package/cwait.md) — 21.4K weekly downloads
- [@ledgerhq/hw-app-cosmos](https://npm.io/package/@ledgerhq/hw-app-cosmos.md) — 4.2K weekly downloads
- [@financial-times/o-loading](https://npm.io/package/@financial-times/o-loading.md) — 2.8K weekly downloads
- [fa](https://npm.io/package/fa.md) — 185 weekly downloads

## Recent versions

- 2.0.0 (latest) — 2022-11-04
- 1.0.2 — 2022-10-30
- 1.0.1 — 2022-10-30

## README

<h1 align="center">promiseTimeout</h1>  

It's the usual setTimeout, but with **promises** or **async/await**.

- - -

### Instalation 🔧 ### 

You can add it as a git submodule to your project:

``` sh
git submodule add 'https://github.com/OWLjz18/promiseTimeout.git'
```

Or you can use the npm module:

``` sh
npm i promise-timeout-jz
```

Or with pnpm:

``` sh
pnpm add promise-timeout-jz
```

- - -

### Use 🔎 ###

#### Syntax: 
``` javascript
promiseTimeout(callback[, delay, { error }]);
```

  * **callback** => It is the callback that will execute after the specified time.
  * **delay?** => It is the time it will take before executing the callback.
  * **error?** => With this parameter we can intentionally reject the promise. What is it for? In case you want to test error handling.

#### Example:

In the examples I will assume you are using Nodejs and the ESmodules.

Way #1: Using **promises**.

``` javascript
import promiseTimeout from 'promise-timeout-jz';

console.log('HTML');

promiseTimeout(() => console.log('CSS'), 2000)
  .then(() => console.log('Javascript'))
  .catch(error => console.error(error));
```

Way #2: Using **async/await**.

``` javascript
import promiseTimeout from 'promise-timeout-jz';

(async () => {
  
  console.log('HTML');
  await promiseTimeout(() => console.log('CSS'), 2000);
  console.log('Javascript');
  
})();
```

Or with top-level await:

``` javascript
import promiseTimeout from 'promise-timeout-jz';

console.log('HTML');
await promiseTimeout(() => console.log('CSS'), 2000);
console.log('Javascript');
```

As a result in any of the three previous examples, we obtain the following by console:

  1. HTML
  2. CSS (after two seconds)
  3. Javascript

Let's look at one last example, where we make the promise to be rejected, using the error parameter:

``` javascript
import promiseTimeout from 'promise-timeout-jz';

console.log('HTML');

promiseTimeout(() => console.log('CSS'), 2000, { error: true })
  .then(() => console.log('Javascript')) // Skip this...
  .catch(error => console.error(error)); // The error is fired and goes into the catcher.
```

**_NOTE:_** In some forums (such as stack overflow, for example), there is already a solution like the following:

``` javascript
const delay = (cb, ms) => new Promise(res => setTimeout(() => res(cb()), ms));
```

And yes, it's a single line instead of a module installation, but... I wanted to do my own. 😹

- - -

### Autor 🦉 ###

  * *__José Zambrano__* ([OWLjz18](https://github.com/OWLjz18)) => Project creator.
    * Instagram => [@owljz18](https://instagram.com/owljz18)
    * Email => <owl.jz18@gmail.com>

- - -

### Support 🤝 ###

If you like the project, you can give us a star. 🌟

- - -

### License 📃 ###

This project is licensed under an _MIT_ license, please visit the [LICENSE.md](./LICENSE.md) file for more information about it.

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