Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 1,000,000+ packages pre-installed, including batchy with all npm packages installed. Try it out:

var batchy = require("batchy")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

batchy v0.1.1

for running asynchronous tasks in batches


Batches asynchronous requests.

How to explain this?

You might have something like this:

getUser(1, function (error, user) {

getUser(2, function (error, user) {

getUser(3, function (error, user) {

This would ordinarily result in 3 individual calls for users:


But what if you wanted to make just one request, with an API like this:


Use batchy:

var getUser = batchy(function (userIds, callback) {
    var ids = (id) { return 'id=' + id; }).join('&');
    $.get('http://.../users?' + ids)
        .done(function (data) {
            callback(undefined, data);
        .error(function (error) {

Batchy will batch requests up into groups of 10, or into time segments of 140ms, which ever comes first.


var mapItem = batchy(map, [options])


  • map(items, [callback(error, mappedItems)]) - a function accepting items and callback. The function is to process the items and call the callback. The result (second argument to callback) is the list of mapped items. This function can also be synchronous if you omit the callback parameter, in which case just return the mapped items.
  • options - can contain:
    • size - the maximum size of each batch (default 10)
    • timeout - the maximum time to wait for items to batch (default 140ms)

It returns a new function that looks like this:

mapItem(item, callback)


  • item - an item to map
  • callback(error, mappedItem) - a function that will be called when the item is eventually mapped.


RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free