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 🙏

© 2025 – Pkg Stats / Ryan Hefner

gate

v0.3.0

Published

An utility to await multiple asynchronous calls in Node environment

Downloads

1,152

Readme

Gate — An utility to await multiple asynchronous calls

Gate is an utility to await multiple asynchronous calls in node.js.

Installing

$ npm install await

Example

var gate = require('gate');
var fs = require('fs');

var latch = gate.latch();
fs.readFile('file1', 'utf8', latch({name: 'file1', data: 1}));
fs.readFile('file2', 'utf8', latch({name: 'file2', data: 1}));

latch.await(function (err, results) {
  if (err) throw err;
  console.log(results[0]); // { name: 'file1', data: 'FILE1' }
  console.log(results[1]); // { name: 'file2', data: 'FILE2' }
});

API

gate module provides following API.

latch([Number count]) -> Function

Return a function which represents a latch. The returned function provides following API.

  • count: Optional. A number of times the returned function must be called before an awaiting callback can start.

([Object mapping][, Boolean skipErrorCheck]) -> Function

Accept an argument mapping definition and return a callback. If a count is given with gate.latch(), the count is decremented.

  • mapping: Optional. An argument mapping definition. The mappipng must be a number or an object. If the mapping is a number, single callback argument is mapped. If the mapping is an object, multiple callback arguments can be mapped. If the mapping is null or undefined, all callback arguments are mapped as Array.

  • skipErrorCheck: Optional. Indicates whether error check is skipped or not. Default value is false.

count() -> Number

Get a current count, if a count is given with gate.latch(). Otherwise, -1 is returned.

val(Object value) -> Object

Wrap a value to distinguish between a value as argument and a mapping index.

  • value: Required. A value.

await(Function callback(err, results)) -> Function

Await all asynchronous calls completion and then run a callback.

  • callback: Required. A callback to run after all asynchronous calls completion.
  • err: Required. An error to indicate any asynhronous calls are failed.
  • results: Required. An array to contain each asynchronous call result as element.

More Examples

Arguments Mapping

Pass an argument index or an object includes argument indexs to a function to be returned from gate.latch(). In the object, values except whose type is number are recognized arguments. To pass an number as argument, wrap it with val function.

var gate = require('gate');
var fs = require('fs');
var exec = require('child_process').exec;

var latch = gate.latch();

// single mapping: arguments[1] in the callback will be result
fs.readFile('file1', 'utf8', latch(1)); 

// multiple mapping: arguments[1] and argments[2] in the callback will be result
exec('cat *.js bad_file | wc -l', latch({stdout: 1, stderr: 2}));

// all mapping: arguments will be result
fs.readFile('file2', 'utf8', latch());

latch.await(function (err, results) {
  if (err !== null) {
    console.log('exec error: ' + err);
  }
  console.log('file1: ' + results[0]);
  console.log('stdout[1]: ' + results[1].stdout);
  console.log('stderr[1]: ' + results[1].stderr);
  console.log('file2: ' + results[2]);
});

Count Down

Pass a count number to gate.latch() to wait until a set of callbacks being made.

var gate = require('gate');
var fs = require('fs');

var files = ['file1', 'file2'];
var latch = gate.latch(files.length);
latch.await(function (err, results) {
  if (err) throw err;
  console.log(results[0]); // { name: 'file1', data: 'FILE1' }
  console.log(results[1]); // { name: 'file2', data: 'FILE2' }
});

process.nextTick(function () {
  files.forEach(function (file) {
    fs.readFile(file, 'utf8', latch({name: file, data: 1}));
  });
});

Error Check Skipping

Pass true as 2nd argument to a function to be returned from gate.latch(). This is useful to check each error one by one.

var gate = require('gate');
var fs = require('fs');

var latch = gate.latch();
fs.readFile('non-existent1', 'utf8', latch({err: 0, data: 1}, true));
fs.readFile('non-existent2', 'utf8', latch({err: 0, data: 1}, true));

latch.await(function (err, results) {
  results.forEach(function (result) {
    if (result.err) {
      console.log(result.err);
    } 
  });
});

Loop

Call a function to be returned from gate.latch() in a loop.

var gate = require('gate');
var fs = require('fs');

var latch = gate.latch();
['file1', 'file2'].forEach(function (file) {
  fs.readFile(file, 'utf8', latch({name: file, data: 1}));
});

latch.await(function (err, results) {
  if (err) throw err;
  console.log(results[0]); // { name: 'file1', data: 'FILE1' }
  console.log(results[1]); // { name: 'file2', data: 'FILE2' }
});