🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

batched

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

batched

Async chaining sugar. Turn async methods into a batch

latest
Source
npmnpm
Version
0.1.1
Version published
Weekly downloads
2
-50%
Maintainers
1
Weekly downloads
 
Created
Source

batched

Async chaining sugar. Turn async methods into a batch

Example

Basically take a hash of asynchronous functions (or methods) and allow you to do serialized batch of commands on it.

A batch "starts" on the nextTick and does each chained command in serial.

If any have a callback that callback will be called. Else a default callback is used that emits an "error" event on the batch if there is an error.

var batched = require("batched")

var asyncThing = {
    _state: {
        1: 1
        , 2: 2
        , 3: 3
    }
    , getAll: function (callback) {
        var state = this._state
        process.nextTick(function () {
            callback(null, state)
        })
    }
    , get: function (id, callback) {
        var state = this._state
        process.nextTick(function () {
            callback(null, state[id])
        })
    }
    , set: function (id, value, callback) {
        var state = this._state
        process.nextTick(function () {
            state[id] = value
            callback(null)
        })
    }
    , del: function (id, callback) {
        var state = this._state
        process.nextTick(function () {
            delete state[id]
            callback(null)
        })
    }
}

batched(asyncThing)
    .set("foo", "bar")
    .set("hello", "world")
    .del("1")
    .del("2")
    .get("3", function (err, result) {
        console.log("result", result)
    })
    .getAll(function (err, results) {
        console.log("results!", results)
    })
    .on("error", function () {
        // No errors!
    })
    .once("finish", function () {
        console.log("finished")
    })

console.log("go chain go!")

Installation

npm install batched

Contributors

  • Raynos

MIT Licenced

FAQs

Package last updated on 04 Dec 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts