pax_global_header00006660000000000000000000000064127544615320014523gustar00rootroot0000000000000052 comment=e8c26046f36962b90e68dc5df33a9672a54b25f5 after-0.8.2/000077500000000000000000000000001275446153200126335ustar00rootroot00000000000000after-0.8.2/.gitignore000066400000000000000000000000261275446153200146210ustar00rootroot00000000000000node_modules .monitor after-0.8.2/.travis.yml000066400000000000000000000001721275446153200147440ustar00rootroot00000000000000language: node_js node_js: - 0.6 - 0.8 - 0.9 - 0.10 - 0.12 - 4.2.4 - 5.4.1 - iojs-1 - iojs-2 - iojs-3 after-0.8.2/LICENCE000066400000000000000000000020321275446153200136150ustar00rootroot00000000000000Copyright (c) 2011 Raynos. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.after-0.8.2/README.md000066400000000000000000000050701275446153200141140ustar00rootroot00000000000000# After [![Build Status][1]][2] Invoke callback after n calls ## Status: production ready ## Example ```js var after = require("after") var db = require("./db") // some db. var updateUser = function (req, res) { // use after to run two tasks in parallel, // namely get request body and get session // then run updateUser with the results var next = after(2, updateUser) var results = {} getJSONBody(req, res, function (err, body) { if (err) return next(err) results.body = body next(null, results) }) getSessionUser(req, res, function (err, user) { if (err) return next(err) results.user = user next(null, results) }) // now do the thing! function updateUser(err, result) { if (err) { res.statusCode = 500 return res.end("Unexpected Error") } if (!result.user || result.user.role !== "admin") { res.statusCode = 403 return res.end("Permission Denied") } db.put("users:" + req.params.userId, result.body, function (err) { if (err) { res.statusCode = 500 return res.end("Unexpected Error") } res.statusCode = 200 res.end("Ok") }) } } ``` ## Naive Example ```js var after = require("after") , next = after(3, logItWorks) next() next() next() // it works function logItWorks() { console.log("it works!") } ``` ## Example with error handling ```js var after = require("after") , next = after(3, logError) next() next(new Error("oops")) // logs oops next() // does nothing // This callback is only called once. // If there is an error the callback gets called immediately // this avoids the situation where errors get lost. function logError(err) { console.log(err) } ``` ## Installation `npm install after` ## Tests `npm test` ## Contributors - Raynos - defunctzombie ## MIT Licenced [1]: https://secure.travis-ci.org/Raynos/after.png [2]: http://travis-ci.org/Raynos/after [3]: http://raynos.org/blog/2/Flow-control-in-node.js [4]: http://stackoverflow.com/questions/6852059/determining-the-end-of-asynchronous-operations-javascript/6852307#6852307 [5]: http://stackoverflow.com/questions/6869872/in-javascript-what-are-best-practices-for-executing-multiple-asynchronous-functi/6870031#6870031 [6]: http://stackoverflow.com/questions/6864397/javascript-performance-long-running-tasks/6889419#6889419 [7]: http://stackoverflow.com/questions/6597493/synchronous-database-queries-with-node-js/6620091#6620091 [8]: http://github.com/Raynos/iterators [9]: http://github.com/Raynos/composite after-0.8.2/index.js000066400000000000000000000012551275446153200143030ustar00rootroot00000000000000module.exports = after function after(count, callback, err_cb) { var bail = false err_cb = err_cb || noop proxy.count = count return (count === 0) ? callback() : proxy function proxy(err, result) { if (proxy.count <= 0) { throw new Error('after called too many times') } --proxy.count // after first error, rest are passed to err_cb if (err) { bail = true callback(err) // future error callbacks will go to error handler callback = err_cb } else if (proxy.count === 0 && !bail) { callback(null, result) } } } function noop() {} after-0.8.2/package.json000066400000000000000000000010471275446153200151230ustar00rootroot00000000000000{ "name": "after", "description": "after - tiny flow control", "version": "0.8.2", "author": "Raynos ", "contributors": [ { "name": "Raynos", "email": "raynos2@gmail.com", "url": "http://raynos.org" } ], "scripts": { "test": "mocha --ui tdd --reporter spec test/*.js" }, "devDependencies": { "mocha": "~1.8.1" }, "keywords": [ "flowcontrol", "after", "flow", "control", "arch" ], "license": "MIT", "repository": "git://github.com/Raynos/after.git" } after-0.8.2/test/000077500000000000000000000000001275446153200136125ustar00rootroot00000000000000after-0.8.2/test/after-test.js000066400000000000000000000041431275446153200162300ustar00rootroot00000000000000/*global suite, test*/ var assert = require("assert") , after = require("../") test("exists", function () { assert(typeof after === "function", "after is not a function") }) test("after when called with 0 invokes", function (done) { after(0, done) }); test("after 1", function (done) { var next = after(1, done) next() }) test("after 5", function (done) { var next = after(5, done) , i = 5 while (i--) { next() } }) test("manipulate count", function (done) { var next = after(1, done) , i = 5 next.count = i while (i--) { next() } }) test("after terminates on error", function (done) { var next = after(2, function(err) { assert.equal(err.message, 'test'); done(); }) next(new Error('test')) next(new Error('test2')) }) test('gee', function(done) { done = after(2, done) function cb(err) { assert.equal(err.message, 1); done() } var next = after(3, cb, function(err) { assert.equal(err.message, 2) done() }); next() next(new Error(1)) next(new Error(2)) }) test('eee', function(done) { done = after(3, done) function cb(err) { assert.equal(err.message, 1); done() } var next = after(3, cb, function(err) { assert.equal(err.message, 2) done() }); next(new Error(1)) next(new Error(2)) next(new Error(2)) }) test('gge', function(done) { function cb(err) { assert.equal(err.message, 1); done() } var next = after(3, cb, function(err) { // should not happen assert.ok(false); }); next() next() next(new Error(1)) }) test('egg', function(done) { function cb(err) { assert.equal(err.message, 1); done() } var next = after(3, cb, function(err) { // should not happen assert.ok(false); }); next(new Error(1)) next() next() }) test('throws on too many calls', function(done) { var next = after(1, done); next() assert.throws(next, /after called too many times/); });