Skip to content

Instantly share code, notes, and snippets.

@DrPizza
Created July 1, 2011 15:17
Show Gist options
  • Save DrPizza/1058757 to your computer and use it in GitHub Desktop.
Save DrPizza/1058757 to your computer and use it in GitHub Desktop.
// perform a simple mechanical transform from this:
{ req, res } = yield app.get(/^\/([0-9a-f]+)$/);
object_id = new bson.ObjectID(req.params[0]);
{ err, db } = yield db.open();
if(err) { return res.send(500); }
store = new mongo.GridStore(db, object_id, "r");
{ err, store } = yield store.open();
if(err || !store.length) { return res.send(404); }
{ err, buf } = yield store.readBuffer(store.length);
res.send(buf, { "Content-Type": store.contentType, "Content-Length": store.length, }, 200);
// into this:
app.get(/^\/([0-9a-f]+)$/, function(req, res) {
object_id = new bson.ObjectID(req.params[0]);
db.open(function(err, db) {
if(err) { return res.send(500); }
store = new mongo.GridStore(db, object_id, "r");
store.open(function(err, db) {
if(err || !store.length) { return res.send(404); }
store.readBuffer(store.length, function(err, buf) {
res.send(buf, { "Content-Type": store.contentType, "Content-Length": store.length, }, 200);
});
});
});
});
// It needs two things; a kind of destructuring assign (converted into callback parameters), and yield (convert the remainder of the function into a callback).
// i.e. a CPS kind of a transform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment