Correctly invoke async callback asynchronously

parent 6b745ca7
2.x
===
* Correctly invoke async callback asynchronously
2.27.2 / 2014-10-28 2.27.2 / 2014-10-28
=================== ===================
......
...@@ -22,6 +22,11 @@ var app = module.exports = {}; ...@@ -22,6 +22,11 @@ var app = module.exports = {};
var env = process.env.NODE_ENV || 'development'; var env = process.env.NODE_ENV || 'development';
/* istanbul ignore next */
var defer = typeof setImmediate === 'function'
? setImmediate
: function(fn){ process.nextTick(fn.bind.apply(fn, arguments)) }
/** /**
* Utilize the given middleware `handle` to the given `route`, * Utilize the given middleware `handle` to the given `route`,
* defaulting to _/_. This "route" is the mount-point for the * defaulting to _/_. This "route" is the mount-point for the
...@@ -132,7 +137,7 @@ app.handle = function(req, res, out) { ...@@ -132,7 +137,7 @@ app.handle = function(req, res, out) {
// all done // all done
if (!layer) { if (!layer) {
done(err); defer(done, err);
return; return;
} }
......
...@@ -130,6 +130,37 @@ describe('app', function(){ ...@@ -130,6 +130,37 @@ describe('app', function(){
}); });
}) })
it('should invoke callback on error', function(done){
var app = connect();
app.use(function (req, res) {
throw new Error('boom!');
});
function handler(req, res) {
res.write('oh, ');
app(req, res, function(err) {
res.end(err.message);
});
}
var server = http.createServer(handler).listen(5559, function(){
http.get({
host: 'localhost',
port: 5559,
path: '/'
}, function(res){
var buf = '';
res.setEncoding('utf8');
res.on('data', function(s){ buf += s });
res.on('end', function(){
buf.should.eql('oh, boom!');
server.close(done);
});
});
});
})
it('should work as middlware', function(done){ it('should work as middlware', function(done){
// custom server handler array // custom server handler array
var handlers = [connect(), function(req, res, next){ var handlers = [connect(), function(req, res, next){
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment