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

syncd_promise_loop

v0.1.6

Published

This utility allows to limit parallel promises, relaunch them with same parameters and rate limit them.

Downloads

2

Readme

** Syncd Promise Loop **

npm i syncd_promise_loop

Allows execution of Promises in parallel workers, that can be limited by the count, or have certain limit which resets after time interval, it can be usefull for e.g. parsing megahuge csv into database, or simply downloading something rate limited.

If you remove timeFrame and limitInTimeFrame parameters, it works without rate limits.

It keeps data object that's shared accross callbacks, so you can work in it with some persistent data.

First args function launches, providing argument for worker, then worker launches a promise, which can be rejected or resolve, if you use a promise there, you can wait for it execution to continue in a worker.

You can retry failed promise by simply calling retry there, if retry is called, worker will retry to do a promise forever.

Finish is destructor, so you can quit database connection there.

Foo bar version of usage:

const inc = require('syncd_promise_loop')

let loop = syncd({
            infoInterval:       23,
            parallel:           12,
            timeFrame:        3000,
            limitInTimeFrame:    3,
        })

        .args((loop)=>{
            loop.counter++
            // Here we set arguments for the loop.
            return {
                counter: loop.counter,
                number: Math.round(Math.random()*loop.multiplier)
            }
        })

        .main((args, loop)=>{
            // Main working loop, that returns a promise
            console.info(`[ > ] Returning promise...`)
            console.info(`       ${loop.counter}`,)
            return new Promise((resolve, reject)=>{

                setTimeout(()=>{
                        resolve(args.number+1)
                }, Math.floor(Math.random()*1000))

                setTimeout(()=>{
                        reject(false)
                }, Math.floor(Math.random()*1000))
            })
        })

        .then(
            (loop, args)=>{
            return {
            resolved: (resolvedWith, args, worker)=>{
                // Here is the function that is called if Promises resolved.
                console.info(`[ > ] Promise resolved:`)
                console.info(`       with:`,resolvedWith)
                console.info(`       args:`,args)
                console.info()
                worker.continueAfterResolving(new Promise((resolve)=>{
                    setTimeout(()=>{
                        console.info(`There it is... ${args.counter}:`, args.number)
                        resolve(`resolving`)

                    },3000)
                }))
            },

            rejected: (rejectedWith, args, retry, worker)=>{
                // Here is the function that is called if Promises resolved.
                retry()
                console.info(`[ > ] Promise rejected:`)
                console.info(`       with:`,rejectedWith)
                console.info(`       args:`,args)
            }
            }})

        .info((loop)=>{
            loop.parameters.infoInterval
            console.info(`[ > ] Info triggered`)
        })

        .until((loop)=>{
            // If true, we no longer stay in loop
            console.info(`[ > ] Until loop triggered`)
            return loop.counter >= 30
        })

        .finish((loop)=>{
            // This gets called, after all workers are resolved
            console.info(`[ > ] Loop is done.`)
        })

    loop.start({
        multiplier: 100,
        counter: 0,
    })

    setTimeout((x)=>{
        console.info(`[ > ] Pausing...`)
        loop.pause()
    }, 5*1000)

    setTimeout((x)=>{
        console.info(`[ > ] Resuming...`)
        loop.resume()
    }, 23*1000)

    console.info(loop)