Skip to content

Instantly share code, notes, and snippets.

View Raynos's full-sized avatar

Jake Verbaten Raynos

View GitHub Profile
"create": function _create(data, cb) {
data._id = uuid();
request({
"uri": this._base_url + "/" + data._id,
"json": data,
"method": "PUT"
}, this._error(cb));
},
// create new post
app.post("/blog", function _create(req, res) {
var post = {
"content": req.body.content,
"title": req.body.title,
"datetime": Date.now(),
"type": "post"
}
model.get(function _get(err, rows) {

I've had to answer flow control issues repeatedly. Both in my own code-base and as answers to StackOverflow questions. All these problems can be handled with one of two methods. One issue is dealing with nested asynchronous tasks and the answer to this is to use middleware. The other issue is aggregating many asynchronous tasks into one end point and the answer is this is to use function execution delay.

I've build a tiny utility function called [after][1] to handle aggregating of asynchronous tasks. I'll walk you through how to use it in this post. I will get [around to][4] building a generic middleware component that can be used to chain asynchronous tasks together effectively, it will be inspired by connect.

Description

Let's start with a description of after. Below is an illustrated example of using it

// Create a single end point to aggregate two asynchronous tasks into.

var cb = after(2, function _allFinished(file1, file2) {

["Batch", "Vow", "Context", "Topic", "Suite"].forEach(function(name) {
module["create" + name] = function(obj) {
return create(module[name]).start(obj);
};
exports[name] = module[name];
exports["create" + name] = module["create" + name];
var lowercase = name.charAt(0).toLowerCase() + name.slice(1, name.length);
exports[lowercase] = module["create" + name];
});
{
"name": "Blaggie-System",
"description": "Blog in pieces",
"version": "0.1.0",
"author": "Jake Verbaten",
"dependencies": {
"express": "2.4.5",
"validator": "0.2.7",
"dust": "0.3.0",
"node-uuid": "1.2.0",
is.suite("vows-is").batch()
.context("a request to GET /")
.topic().is.a.request("GET /")
.vow("returns 200").it.should.have.status(200)
.vow("returns text/html").it.should.have
.header("content-type", "text/html; charset=utf-8")
.context("contains a body")
.topic().is.property('body')
.vow("that exists").it.should.be.ok
//handles audio information of clients, eg. codecs they can play
module.exports = function(availClients, unavailClients, io) {
var util = require('util');
//for each connection
io.sockets.on('connection', handleConnection);
function handleConnection (socket) {
//fired when the client discovered it's codec capabilities
socket.on('codecCapability', function (mp3, mp4, ogg) {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: uuid() }));
app.use(app.router);
module.exports = function _route(app, middle) {
app.get("/blog", [
middle.data.getAllPosts,
middle.output.renderPosts
]);
app.get("/blog/new", [
middle.input.cleanseUrl,
middle.auth.requireLogin,
app.get("/blog/:postId/edit", [
middle.input.cleanseUrl,
authorized,
checkId,
middle.data.getPostById,
middle.output.renderPostEditForm
]);
// checkId could be a app.param("postId", checkId);