Skip to content

Commit 806ed71

Browse files
author
Peter Marton
authored
feat(chain): schedule handlers to the next tick (#1798)
1 parent c5a07c5 commit 806ed71

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

lib/chain.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,9 @@ function call(handler, err, req, res, _next) {
161161
return;
162162
} else if (!hasError && arity < 4) {
163163
// request-handling middleware
164-
handler(req, res, next);
164+
process.nextTick(function nextTick() {
165+
handler(req, res, next);
166+
});
165167
return;
166168
}
167169

test/chain.test.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
22
/* eslint-disable func-names */
33

4+
var domain = require('domain');
45
var Chain = require('../lib/chain');
56

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

186187
test('throws error for double next calls in strictNext mode', function(t) {
187-
var doneCalled = 0;
188+
t.expect(1);
188189
var chain = new Chain({
189190
strictNext: true
190191
});
@@ -194,7 +195,15 @@ test('throws error for double next calls in strictNext mode', function(t) {
194195
next();
195196
});
196197

197-
try {
198+
var testDomain = domain.create();
199+
200+
testDomain.on('error', function onError(err) {
201+
t.equal(err.message, "next shouldn't be called more than once");
202+
testDomain.exit();
203+
t.done();
204+
});
205+
206+
testDomain.run(function run() {
198207
chain.run(
199208
{
200209
startHandlerTimer: function() {},
@@ -206,14 +215,9 @@ test('throws error for double next calls in strictNext mode', function(t) {
206215
{},
207216
function(err) {
208217
t.ifError(err);
209-
doneCalled++;
210-
t.equal(doneCalled, 1);
211-
t.done();
212218
}
213219
);
214-
} catch (err) {
215-
t.equal(err.message, "next shouldn't be called more than once");
216-
}
220+
});
217221
});
218222

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

0 commit comments

Comments
 (0)