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 🙏

© 2026 – Pkg Stats / Ryan Hefner

asyncback

v1.2.22

Published

Convert async function to callback accepting funtion

Readme

asyncback

npm downloads Open issues Pull requests

Convert async functions to callback style functions.

Why ?

With the introduction of async/await, writing async code has become much simpler. However, there still exists Javascript API that requires you to pass in callback accepting functions. For example, ExpressJS requires middleware functions to be plain old functions that accept a next callback.

The asyncback module converts an async function to a callback accepting function.

In Short

This:

async function(arg1, arg2, .., argN)

is converted to:

function(arg1, arg2, .., argN, callback)

Which is awesome in cases like writing an expressjs request handler, while using mongoose models:

const asyncback = require('asyncback');

app.get('/users', asyncback(async (req, res) => {
    const users = await User.find({ 'banned': false });
    const offer = await Offers.findOne({ 'active': true });
    res.json({ 'users': users, 'offer': offer });
}));

Usage

Install module:

npm install asyncback --save

Wrap async functions with asyncback before registering them with ExpressJS:

const asyncback = require('asyncback');

app.get('/path', asyncback(async (req, res) => {
    // async await style code
}));

Better yet, if middleware functions are in separate files, simply export the wrapped function:

const asyncback = require('asyncback');

async function someMiddleware(req, res){
    // async await style code
}

module.exports = asyncback(someMiddleware);

Also, if the middleware decides to stop next callback from being called:

const asyncback = require('asyncback');

app.get('/file', asyncback(async (req, res) => {
    // async await style code
    // start streaming, proceed no further
    return asyncback.NO_CB;
}));

Notes

  1. The callback will be automatically called after async function returns.
  2. Any error thrown by the async function is passed as the first parameter to callback.
  3. The return value of async function is passed as the second parameter to callback.
  4. In cases where the callback should not be automatically called, the async function can return a special value: asyncback.NO_CB.

Flexibility

The asyncback function is really generic that it converts an async function into a callback style function which accepts a callback as its last parameter. Hence its usage is not limited to just ExpressJS middleware. This also works for functions that return a promise or promise-like result.

Safety

The asyncback can safely wrap non async/promised functions and the returned function can be invoked without the callback parameter. In such cases the wrapped function will be called synchronously and with all supplied arguments. However such usage is discouraged as it serves no beneficial purpose.

Licence

The source code is available under MIT Licence.