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

gensy

v0.1.4

Published

Asynchronous utils focused on generators

Downloads

436

Readme

GENSY

Asynchronous utils for Node.js/IO.js focused on generators.

1. Installation

Just run the following:

npm install gensy

Remember to add gensy as a dependency in your package.json file:

"dependencies": {
	...
	"gensy": "*"
	...
}

2. Usage

Simple generator

If you just want to run a generator to run some asynchronous code as if it were synchronous (by mean of the yield statement), just call gensy as follows:

var gensy = require('gensy');

gensy(function* (next) {
	try {
		...
		var x = yield foo(next);
		...
	} catch (error) {
		console.warn('Error:', error);
	}
});

Where foo is supposed to be an asynchronous function that uses next as a callback following the error+result arguments convention:

function foo(callback) {
	...
	return callback(error, result);
}

If an error is returned by foo, then generator's catch block will be executed.

This usage supports a callback to listen generator end. To use it the generator must call done() (a second argument) when finished:

var gensy = require('gensy');

function* genA(next, done) {
	try {
		var x = yield foo(next);
		...
		return done();
	} catch (error) {
		console.warn('Error:', error);
	}
}

gensy(genA);

Support both, callbacks and generators

In order to use a simple function from a generator, it has to accept an argument where the next generator is passed. This is a problem if that functions are used elsewhere but receiving a callback instead of a generator, and we want to keep that compatibility.

Gensy can be used then to preprocess the callback, so generators can be passed, too:

function passMeCallbacksOrGenerators (thing) {
	callback = gensy.callback(thing);
	// ... Do some async work
	callback(error, result);
}

The previous code ensures that received callback can be both a function or a generator. The transformation allows us to use it as a simple callback, where the first argument is the error, and the second one is the returning data. It is important to keep this convention. If an error occurs and we call callback(error), then our caller generator will fall in the catch statement, receiving it.

So now, this function can be used passing both, generators or traditional callbacks:

passMeCallbacksOrGenerators(function (error, result) {
	// I'm a traditional callback
});
gensy(function* (next) {
	try {
		var x = yield passMeCallbacksOrGenerators(next);
		// I'm a generator, with yield statements
	} catch (error) {...}
});

Generator series

To run a list of generators, one by one, use gensy.series() as follows:

var gensy = require('gensy');

function* genA (next, done) {
	try {
		var x = yield foo(next);
		...
		return done();
	} catch (error) {
		console.warn('Error:', error);
	}
}

function* genB (next, done) {
	try {
		var x = yield foo(next);
		...
		return done();
	} catch (error) {
		console.warn('Error:', error);
	}
}

gensy.series([genA, genB], function () {
	console.log('Series ended');
});