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

patter

v0.1.2

Published

Utilities for iterating through arrays using promises

Downloads

9

Readme

Patter: Utilities for iterating through arrays using promises

Implementations of common functional methods, such as map, filter and forEach, but allowing the iteration functions to return promises.

In other words, similar to the excellent async module, but using promises instead of callbacks.

Example

Using a supported promise implementation, such as q:

var promises = require("patter").q();

function iterator(element, index) {
    return promises.resolved(index + ": " + element);
}

return promises.map(["apple", "banana"], iterator)
    .then(function(value) {
        assert.deepEqual(value, ["0: apple", "1: banana"]);
    });

API

Patter doesn't include a promise implementation, and instead must be created with a promise implemention.

For libraries that are directly supported by Patter, just call the appropriate function:

  • require("patter").q() -- use q
  • require("patter").promise() -- use promise

Note that patter does not depend on any promise implementation. In other words, if you want to use, say, q with patter, make sure that you install both the patter and q modules.

To create an instance of Patter, call require("patter") with a function createPromise(callback), where callback has the signature callback(resolve, reject). For instance, to use q manually:

var q = require("q");

function createPromise(func) {
    var deferred = q.defer();
    
    func(
        deferred.resolve.bind(deferred),
        deferred.reject.bind(deferred)
    );
    
    return deferred.promise;
}

var promises = require("patter")(createPromise);

// Use promises.map, promises.forEach, and so on;

(Note that the above won't work in older versions of q due to a bug.)

forEach(array, iterator)

Apply iterator to every element in array in parallel. If one of the promises returned by an application is rejected, the overall promise is rejected.

Arguments:

  • array -- an array to iterate over.
  • iterator(element, index) -- the iterator is passed each element of the array and its index. Should return a promise.

forEachSeries(array, iterator)

The same as forEach, except that iterator is only called on each element once the previous application of iterator has finished.

map(array, iterator)

Returns a promise of an array of values by applying iterator to every element in array in parallel. If one of the promises returned by an application is rejected, the overall promise is rejected.

Arguments:

  • array -- an array to iterate over.
  • iterator(element, index) -- the iterator is passed each element of the array and its index. Should return a promise of the mapped value.

mapSeries(array, iterator)

The same as map, except that iterator is only called on each element once the previous application of iterator has finished.

filter(array, iterator)

Returns a promise of an array of values that satisfy iterator. If one of the promises returned by an application is rejected, the overall promise is rejected.

Arguments:

  • array -- an array to iterate over.
  • iterator(element, index) -- the iterator is passed each element of the array and its index. Should return a promise of a boolean, true if the element should be in the new array, false otherwise.

filterSeries(array, iterator)

The same as filter, except that iterator is only called on each element once the previous application of iterator has finished.