npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

promise-timeout-and-looper

v1.0.6

Published

javascript sleep using promises and looper method

Downloads

6

Readme

Promise timeout and looper

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

// 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

looper

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

Here is a code fragment in 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