From 806ed7119db9ed4cce77aef3d898aae561224dd8 Mon Sep 17 00:00:00 2001 From: Peter Marton Date: Wed, 31 Jul 2019 15:07:10 -0700 Subject: [PATCH] feat(chain): schedule handlers to the next tick (#1798) --- lib/chain.js | 4 +++- test/chain.test.js | 20 ++++++++++++-------- 2 files changed, 15 insertions(+), 9 deletions(-) diff --git a/lib/chain.js b/lib/chain.js index 59314d5de..d710c8c5d 100644 --- a/lib/chain.js +++ b/lib/chain.js @@ -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; } diff --git a/test/chain.test.js b/test/chain.test.js index 80ca19946..a714b228d 100644 --- a/test/chain.test.js +++ b/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']) { @@ -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 }); @@ -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() {}, @@ -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) {