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

cbify

v1.0.0

Published

Wraps synchronous functions with a callback-style API

Downloads

36,917

Readme

cbify

Wraps synchronous functions with a callback-style API so they can match their async brethren.

Useful for handling both sync and async methods with the same underlying code. Preserves the sync-ness of the original function, making it possible to use values passed to the callback as a return value.

Install

npm

npm install --save cbify

Bower

bower install --save cbify

The browser build for bower includes a UMD wrapper which adapts to various module systems, or exposes a cbify global if none are present.

Duo

var cbify = require('es128/cbify')

Usage

var cbify = require('cbify');

var sum = cbify(function (a, b) {
	return a + b;
});

var answer;
sum(32, 96, function (err, result) {
	answer = result;
});

console.log(answer); // 128
// would have been undefined if `sum` had handled the callback asynchronously

If provided a function whose last named argument is cb or callback (or even if it just contains callback), then that function will be returned unchanged.

The this context the cbify'd function is called with will be preserved for the underlying function. Feel free to use bind, apply, etc as you would have before implementing cbify.

Similar modules

I was surprised I couldn't find a pre-existing module that did this. I did find a few that almost did it, but were ruled out for slight differences.

  • sinless: Uses setImmediate, causing the wrapped function to always return asynchronously, even though the underlying method may still be blocking.
  • ifyify: The callbackify method provided by this module is only different in that it wraps the function with a continuation-style API, meaning that err will now always be the first argument.
  • wrap-fn: Another API style. Expects the callback at the time the function is being wrapped so the resulting function signature stays the same.

What about Z͡alg̨ó?

This makes synchronous functions pass their result to a callback synchronously. It's an important feature of this module, providing the ability to adapt some async APIs to also provide a sync option with minimal code changes. This is done knowingly and is consistent, so it does not release Zalgo.

However, you do have to be careful for ͟h͞e͘ ̢Wa͜it̛s̨ ͡B̨e͡h̛in̨d ͠The̷ W͏a͝l͏ĺ. If you do not know whether the functions you're passing into cbify are sync or async, and you are otherwise treating them identically, then you may be ́un͘l͜͝e҉a͟҉̨sh̕i̶͜҉n͏̧̕g̢̕ ̧T̷͞ḩe͟ ͜N̢̛͢e̛͟͠z̨͟ṕ̵̨e͟͡͏r̡̀d̨i̧̧a̢͢n ̡hi҉͜v̷e͢-̡͘͘mi̵͞nd̀ ̡of̀ ͢͝cḩ̕a̶̶o̷͜s͘͞.҉͝. In that case, you may want to use sinless instead, or use cbify together with dezalgo.

License

ISC