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

koa-async

v1.0.0

Published

A Koa wrapper for caolan/async.

Downloads

34

Readme

koa-async

npm version

A Node.js Koa wrapper for caolan/async.

Requirements

  • Node.js v4.x+ for ES6 generators support

Usage

First, install the package using npm:

npm install koa-async --save

Then, require the koa-async package and add the yield keyword before each async invocation, omitting the "done" callback function, as demonstrated here:

var async = require('koa-async');

 // A sample list of items to process
var items = [1, 2, 3];
    
// Example 1: Batch-processing items in parallel
yield async.each(items, function(item, cb) {
    // Call this when done processing the current item
    cb();
});

// Example 2: Filtering items in parallel
var filter = yield async.filter(items, function(item, cb) {
    // Filter out items that are not equal to 2
    cb(null, item === 2);
});

// The following will output [2]
this.body = { result: filter };

Koa Example

Here's a more complete example that includes creating a basic Koa app and using various async methods (also available in examples/async.js):

var async = require('koa-async');

// Create sample app
var app = require('koa')();

// Sample middleware
app.use(function* () {
    // A sample list of items to process
    var items = [1, 2, 3];
    
    // Example 1: Batch-processing items in parallel
    yield async.each(items, function(item, cb) {
        // Call this when done processing the current item
        cb();
    });
    
    // The following will output only after the items are done processing in parallel
    console.log('[Test]', 'async.each:', 'done');
    
    // Example 2: Filtering items in parallel
    var filter = yield async.filter(items, function(item, cb) {
        // Filter out items that are not equal to 2
        cb(null, item === 2);
    });
    
    // The following will output [2]
    console.log('[Test]', 'async.filter:', filter);
    
    try {
        // Example 3: Error handling in an async function
        yield async.each(items, function(item, cb) {
            // Purposely fail on the last item
            if (item == 3) {
                // Invoking the callback with an error will cause
                // the control flow to shift to the catch block
                cb('This is a test error message.');
            }
        });
    }
    catch (err) {
        // One of the items invoked the callback with an error
        console.log('[Test]', 'async.error:', err);
    }
    
    // Use any other async function as documented in the async repo:
    // https://github.com/caolan/async
    // Just make sure to strip out the last callback function in each
    // method -- koa-async adds its own callback function on your behalf
});

// HTTP port
var port = process.env.PORT || 3000;

// Listen for connections
app.listen(port);

// Log port
console.log('Server listening on port ' + port);

Run the script and visit http://localhost:3000/ to test it out. Check your Node.js console to see the test output.

License

Apache 2.0