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

make-async

v0.0.0

Published

Allows writing simple synchronous function an use them later as asynchronous

Downloads

1

Readme

make-async

Allows writing simple synchronous function an use them later as asynchronous.

Sometimes, code written in syncronous style looks better, but when you need to use it as asynchronous function, you always need to check for callback presence, manage, when to return result — schedule with process.nextTick(), setImmediate, or call it directly.

make-async takes this problem away, and you can write bunch of simple synchronous functions, that return result or error as an instance of Error class. Moreover, when you wrap your synchronous functions, you can specify when callback should be called (see "When to call back" section).

Installation

npm install make-async

Usage

// Require make-async
var makeAsync = require('make-async'),
    asyncSum;

// Synchronous function to wrap
function sum(a, b) {
    return a + b;
}

// Wrap our synchronous function
asyncSum = makeAsync(sum);

// When we call it
asyncSum(1, 2, function (err, result) {
    // err will be null
    // result will equal to 3
});

Dealing with errors

If wrapped sync function returns an instance of Error, it will be passed as first argument to provided callback.

// Require make-async
var makeAsync = require('make-async'),
    asyncErrorFunc;
    
function errorFunc() {
    return new Error('Some error');
}

asyncErrorFunc = makeAsync(errorFunc);

asyncErrorFunc(function (err, result) {
    // err will be instance of Error returned from sync function
    // result will be null
});

When to call back

There is optional second argument to makeAsync that determines, when callback function will be actually called. Possible values are:

  • 'immediate' (default) — callback will be called using setImmediate function provided by node v0.10. If setImmediate is not defined, if will fall back to 'nextTick'.
  • 'nextTick' — callback will be called using process.nextTick()
  • 'direct' — callback will be called immediately

'this' variable

Don't rely on this variable in your functions — makeAsync calls your functions with null value as this value. Just write functions which rely only on their arguments to produce return.

Tests

Tests are written in mocha, and placed in /test folder. To run them, just call mocha in shell.

$ mocha