# promise-timeout-and-looper

> javascript sleep using promises and looper method

Latest version **1.0.6** (published 2020-02-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install promise-timeout-and-looper
pnpm add promise-timeout-and-looper
yarn add promise-timeout-and-looper
bun add promise-timeout-and-looper
```

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.6 |
| Published | 2020-02-27 |
| First published | 2020-02-17 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 70.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | The Zach |
| Maintainers | zach2825 |
| Keywords | promise, timeout, loops, timed, react, helper, vue, ticks |

## Links

- npm: https://www.npmjs.com/package/promise-timeout-and-looper
- Repository: https://github.com/zach2825/promise-timeout-and-looper
- npm.io page: https://npm.io/package/promise-timeout-and-looper

## Dependencies (1)

- [lodash](https://npm.io/package/lodash.md) ^4.17.15

## Alternatives

- [mobx-react](https://npm.io/package/mobx-react.md) — 2.8M weekly downloads
- [rc-tree](https://npm.io/package/rc-tree.md) — 2.6M weekly downloads
- [@react-oauth/google](https://npm.io/package/@react-oauth/google.md) — 1.3M weekly downloads
- [@wagmi/connectors](https://npm.io/package/@wagmi/connectors.md) — 877.0K weekly downloads
- [vee-validate](https://npm.io/package/vee-validate.md) — 836.4K weekly downloads

## Recent versions

- 1.0.6 (latest) — 2020-02-27
- 1.0.5 — 2020-02-27
- 1.0.4 — 2020-02-27
- 1.0.3 — 2020-02-27
- 1.0.2 — 2020-02-21
- 1.0.1 — 2020-02-18
- 1.0.0 — 2020-02-17

## README

# Promise timeout and looper
<p align="center">
<img src="images/promise-timeout-and-looper.png" alt="Promise timeout and looper">
</p>

An easy way to do looping ticks at a set time interval. Other ways to do this requires recursion. This package eliminates that.

You'll need to initialize the sleeper or looper to use it. That's done because you can cancel the timeout however you'd like. like in reacts componentWillUnmount or vue unmounted hook. You can see a few examples in the examples directory.

## Installation

```
npm install --save promise-timeout-and-looper
```

or

```
yarn add promise-timeout-and-looper
```

## Usage

### sleeper

adds an intuitive sleep method to your js projects

```js
// Import sleeper
const {sleeper} = require('promise-timeout-and-looper');

const test = async = () => {
    
    // Initialize the 5 second timeout
    const sleep_obj = sleeper(5000, {
        //optional
        debug: false, // default is false
        message: 'optional message here', // this messages is massed through the tick resolve. can be used to know which tick is resolving if you have multiple sleep_objects
    });
    
    // tick gets the it of setTimeout, message, the ms value, all previous ids of setTimeout, and tickCount. 
    const tick = await sleep_obj.tick();
    
    //this could also look like this in es syntax 
    // const {tickCount} = await sleep_obj.tick(); // on first run tickCount would equal 0
}
```

if you define the sleep_obj globally then you can cancel the running tick from anywhere at any time. So in like in react if you set the sleep_object in state in your *componentWillUnmount* hook you could add a call like 
this `this.state.sleep_obj && this.state.sleep_obj.cancel_timer()` that will invalidate the timer and call clearTimeout on the latest timer id in the registry.


You can find anohter example [here](__tests__/sleeper.test.js)

### looper

easy to use loop that adds in interval after your callback has resolved

Here is a code fragment in vue

```vue
import {looper} from 'promise-timeout-and-looper';

export default {
    data() {
        return {
            loop: null,
            numbers: [],
        };
    },
    methods: {
        async start() {
            this.numbers = [];
            console.log("starting");

            // loopStart returns a promise that resolves when loopCancel or loop_tick_callback returns false
            await this.loop.loopStart({
                debug: true, // optional defaults to false

                // required
                ms: 1000,
                loop_tick_callback: async (tick) => {
                    // tick gets the sleeper object plus if the tick is valid or not.
                    const {tickCount} = tick;
                    this.numbers.push(tickCount);

                    if (tickCount === 5) {
                        return false;
                    }
                }
            });

            console.log('done');
        },
        async cancel() {
            // cancels current tick
            this.loop && this.loop.loopCancel();
        },
    },
    mounted() {
        this.loop = looper;
    },
    beforeDestroy() {
        this.cancel();
    }
}
``` 

You can find a few examples of looper here
* [react](examples/react.looper.jsx)
* [vue](examples/vue.looper.vue)
* [test](__tests__/sleeper.test.js)

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