Skip to content

Instantly share code, notes, and snippets.

@hns
Created December 8, 2009 18:10
Show Gist options
  • Save hns/251860 to your computer and use it in GitHub Desktop.
Save hns/251860 to your computer and use it in GitHub Desktop.
exports.app = function myAsyncApp(request){
return function(responder) {
responder.init(200, {});
var i = 0;
var intervalId = setInterval(function(){
responder.write("Every second another message");
if(i++ == 10){
responder.close();
clearInterval(intervalId);
}
}, 1000);
};
}
exports.middleware = function(app) {
return function(request) {
var response = app(request);
if (typeof response === "function") {
return function(responder) {
// create a proxy for the responder
var proxy = createResponderProxy(responder);
// ... and overwrite whatever method you want
proxy.[init|write|close] = ...;
response(proxy);
};
} else {
// sync
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment