Skip to content

Instantly share code, notes, and snippets.

@octaflop
Last active December 14, 2015 02:49
Show Gist options
  • Select an option

  • Save octaflop/5016410 to your computer and use it in GitHub Desktop.

Select an option

Save octaflop/5016410 to your computer and use it in GitHub Desktop.
working example of hand-written async on a redis server
exports.list = function(req, cb) {
client.get(MODELSET_ID + ":blog:entries", function(err, blogLen) {
if (err) { console.log(err); }
var retList = [];
function done (retBlog) {
console.log("pushing id: " + retBlog.metadata._id);
retList.push(retBlog);
blogLen--;
if (blogLen <= 0) { cb(retList); }
}
for (var id = 1; id <= blogLen; id++) {
blogById(id, function(retBlog) {
done(retBlog);
});
}
});
};
function blogById(blogId, cb) {
var len = 4; // The number of calls we'll be making
var obj = {};
function callback () {
console.log("cb len: " + len);
len--;
if (len <= 0) { cb(obj); }
}
client.hgetall(MODELSET_ID + ":blog:entry:" + blogId, function(err, reply) {
obj["metadata"] = reply;
console.log('metadata ' + reply);
callback();
});
client.hgetall(MODELSET_ID + ":blog:entry:" + blogId + ":content", function(err, reply) {
obj["content"] = reply;
console.log('content ' + reply);
callback();
});
client.hgetall(MODELSET_ID + ":blog:entry:" + blogId + ":author", function(err, reply) {
obj["author"] = reply;
console.log('author ' + reply);
callback();
});
client.hgetall(MODELSET_ID + ":blog:entry:" + blogId + ":datetime", function(err, reply) {
obj["datetime"] = reply;
console.log('datetime ' + reply);
callback();
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment