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

antiquefy

v0.1.2

Published

Create callback-last style functions from functions returning promises and from generator functions.

Downloads

7

Readme

Antiquefy

Create callback-last style functions from functions returning promises and from generator functions.

Installation

Install from NPM:

npm install antiquefy

Require Antiquefy in your code:

var antiquefy = require('antiquefy');

Examples

In most cases asynchronous functions which return their results in a callback function can be easily promisified using various promise libraries. However, if you have a 3rd party function which takes a hook-like function with a done callback, the process is not so straightforward.

For example, passport-local strategy takes a verify callback function which is itself supplied with a done callback. Antiquefy handles the calling of the done callback automatically after the antiquefied function has been finished. The done callback is expected to be an error first style callback.

Generators

Here is the antiquefied version of the Passport local strategy example using generators:

passport.use(new LocalStrategy(antiquefy(
	function*(username, password) {
		var user = yield User.findOne({ username: username });

		if (!user) {
			return false;
		}
		
		if (!user.verifyPassword(password)) {
			return false;
		}
		
		return user;
	}
)));

Promises

The above could also be achieved using promises:

passport.use(new LocalStrategy(antiquefy(
	function(username, password) {
		return User.findOne({ username: username }).then(function(user) {

			if (!user) {
				return false;
			}
		
			if (!user.verifyPassword(password)) {
				return false;
			}
		
			return user;
		});
	}
)));

Functionally this is equivalent to the generator example, but is little more verbose and requires one extra indentation level.

API

antiquefy(fn, [options])Function

Convert the given function or generator function into a function which takes a done callback.

| Param | Type | Description | | --- | --- | --- | | fn | Function or GeneratorFunction | The function (or generator function) to Antiquefy. | | options | Object | Options for Antiquefy. | | options.spread | boolean | Set this to true if you wish to spread the returned array elements as individual arguments for the done callback.OptionalDefault: false |

Spreading arguments

If the done callback takes more than one parameter, it can be supplied multiple arguments by returning an array and setting the spread option to true.

var sum = antiquefy(function(a, b) {
	return [a, b, a + b];
}, { spread: true });

sum(4, 6, function(error, first, second, sum) {
	console.log(first); // 4
	console.log(second); // 6
	console.log(sum); // 10
});

This works similarly to Bluebird's asCallback(..) and that is what Antiquefy uses internally.

Tests

Unit tests can be run by executing:

npm test