New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details
Socket
Book a DemoSign in
Socket

@subiz/flow

Package Overview
Dependencies
Maintainers
2
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@subiz/flow

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

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
50
-23.08%
Maintainers
2
Weekly downloads
 
Created
Source

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)

Keywords

async

FAQs

Package last updated on 04 May 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts