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

@subiz/flow

v1.1.0

Published

[![npm][npm]][npm-url] [![deps][deps]][deps-url] [![size][size]][size-url]

Downloads

145

Readme

npm deps size

@subiz/flow

Just a few simple functions to help you write asynchronous functions easier

  • sleep, pauses the current execution flow for a specific milliseconds.
  • loop, repeatly calls a function until it returns false.
  • map, runs a function concurrently on every items of the given collection.
  • batch, executes a function agains a stream of jobs (unbounded) in windowed fashion.

Installing

npm i --save @subiz/flow

Examples

Sleep

Sleep pauses the current execution flow

var flow = require('@subiz/flow')

console.log('sleeping for 1 sec')
await flow.sleep(1000)
console.log('ok, I am up')

Loop

Loop is just like do/while, but it runs on both sync and async function

var flow = require('@subiz/flow')

var n = 0
// call an async function 10 times
await flow.loop(async () => {
   	await flow.sleep(10)

   	n++
   	if (n === 10) return false
   	return true
})

console.log(n) // 10

Caution: The above code is just a super complicated version of

for (var i = 0; i < 10; i++) await doSomethind()

The loop function only make sense when you have to write code that run on older browsers which do not support async/await (and you hate babel). Here is an example of asynchronous looping without async/await

var n = 0
// call a async function 10 times
flow.loop(function() {
	return flow.sleep(10).then(function() {
		n++
		return n === 10
	})
}).then(function() {
	console.log(n) // 10
})

But again, please do not use this function when a simple for-loop will do.

Map

Map is like js array.prototype.map but run concurrently.

var flow = require('@subiz/flow')

outs = await flow.map([1, 2, 3, 4], async i => {
   	await flow.sleep(1000)
   	return i * 2
}, 2)

// after 2 seconds
console.log(outs) // [2, 4, 6, 8]

On the example above, we have 4 items to process, each item need 1 second to process. With concurrency level of 2, it would only takes 2 seconds to finish.

Batch

References

sleep(ms)

loop(func)

map(collections, func, concurrencyLevel)

batch(maxItemsPerBatch, delayMilliseconds, func)