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

pync

v1.0.3

Published

Utilities for promises pync ~= promise + async

Downloads

1,160

Readme

pync

Utilities for promises pync ~= promise + async

Promise.all() is very useful but it runs all promises in parallel. Pync solves that.

For example we have a function like this:

function somethingAsync(value) {
  return new Promise((resolve, reject) => {
    console.log('starting', value)
    setTimeout(() => {
      console.log('finishing', value)
      resolve(`result ${value}`)
    }, 100)
  })
}

Then we use Promise.all():

Promise.all([1, 2, 3].map((value) => somethingAsync(value)))
  .then((results) => console.log('done', results))

We will see:

starting 1
starting 2
starting 3
finishing 1
finishing 2
finishing 3
done [ 'result 1', 'result 2', 'result 3' ]

This means all promises are started at once. What if we want to run one after another? Imagine you have an array of URLs and you want to download the files pointed by them. You probably don't want to download all at once. pync solves that with these methods:

pync.series(arr, func)

const pync = require('pync')

const arr = [1, 2, 3]
pync.series(arr, (value) => somethingAsync(value))
  .then(() => console.log('done'))

This is the output:

starting 1
finishing 1
starting 2
finishing 2
starting 3
finishing 3
done

pync.map(arr, func)

const pync = require('pync')

const arr = [1, 2, 3]
pync.map(arr, (value) => somethingAsync(value))
  .then((results) => console.log('done', results))

This is the output:

starting 1
finishing 1
starting 2
finishing 2
starting 3
finishing 3
done [ 'result 1', 'result 2', 'result 3' ]

pync.dict(arr, func)

Given an array of strings it will call func for each string and finally construct an object with all the keys mapped to the values returned by func.

const pync = require('pync')

const arr = ['foo.txt', 'bar.txt']
pync.dict(arr, (filename) => readFileAsync(filename))
  .then((results) => console.log('done', results))

This is the output:

done { 'foo.txt': 'contents of foo.txt', 'bar.txt': 'contents of bar.txt' }

pync.whilst(test, func[, initialValue])

It executes a given function func whilst a given function test returns a truly value. The test function must be synchronous and it's always evaluated before func. You can pass an initial value, in which case both the test and func functions will receive it as an argument. Then, the value returned by func will be passed to the next iteration, so both test and func will receive it. And when finally test returns a falsy value, the latest value returned by func will be returned by pync.whilst()

const pync = require('pync')

const test = (totalAffectedRows) => totalAffectedRows < 1000
const func = (totalAffectedRows) => db.someBatchUpdate().then((result) => result.affectedRows + totalAffectedRows)
pync.whilst(test, func, 0)
  .then((totalAffectedRows) => console.log('total affected rows', totalAffectedRows))

Index

All iteratees receive the current iteration number as second parameter.

Installing

npm install pync --save