Skip to content

Commit

Permalink
feat(chain): schedule handlers to the next tick (#1798)
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Marton committed Jul 31, 2019
1 parent c5a07c5 commit 806ed71
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/chain.js
Expand Up @@ -161,7 +161,9 @@ function call(handler, err, req, res, _next) {
return;
} else if (!hasError && arity < 4) {
// request-handling middleware
handler(req, res, next);
process.nextTick(function nextTick() {
handler(req, res, next);
});
return;
}

Expand Down
20 changes: 12 additions & 8 deletions test/chain.test.js
@@ -1,6 +1,7 @@
'use strict';
/* eslint-disable func-names */

var domain = require('domain');
var Chain = require('../lib/chain');

if (require.cache[__dirname + '/lib/helper.js']) {
Expand Down Expand Up @@ -184,7 +185,7 @@ test('onceNext prevents double next calls', function(t) {
});

test('throws error for double next calls in strictNext mode', function(t) {
var doneCalled = 0;
t.expect(1);
var chain = new Chain({
strictNext: true
});
Expand All @@ -194,7 +195,15 @@ test('throws error for double next calls in strictNext mode', function(t) {
next();
});

try {
var testDomain = domain.create();

testDomain.on('error', function onError(err) {
t.equal(err.message, "next shouldn't be called more than once");
testDomain.exit();
t.done();
});

testDomain.run(function run() {
chain.run(
{
startHandlerTimer: function() {},
Expand All @@ -206,14 +215,9 @@ test('throws error for double next calls in strictNext mode', function(t) {
{},
function(err) {
t.ifError(err);
doneCalled++;
t.equal(doneCalled, 1);
t.done();
}
);
} catch (err) {
t.equal(err.message, "next shouldn't be called more than once");
}
});
});

test('calls req.startHandlerTimer', function(t) {
Expand Down

0 comments on commit 806ed71

Please sign in to comment.