Skip to content

Commit 2fff24b

Browse files
committed
fix issue with return next()
1 parent e6f2ade commit 2fff24b

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

lib/handler.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
async function dispatch(middleware, state, i = 0) {
44
const fn = middleware[i] || Function.prototype;
5-
const next = (err, context, event) => {
5+
const next = async (err, context, event) => {
66
if (err) {
7-
return Promise.reject(err);
7+
throw err;
88
}
99

1010
if (context !== undefined) {
@@ -15,7 +15,7 @@ async function dispatch(middleware, state, i = 0) {
1515
state.event = event;
1616
}
1717

18-
return dispatch(middleware, state, i + 1);
18+
await dispatch(middleware, state, i + 1);
1919
};
2020

2121
const result = await fn(state.event, state.context, next);

test/lambda-handler-tests.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ describe('handler', function() {
212212
});
213213
});
214214

215-
it('middleware can override result after next()', function() {
215+
it('middleware can override result after await next()', function() {
216216
const stub = sinon.stub().returns('foo');
217217
const fixture = lambdaHandler()
218218
.use(async (event, context, next) => {
@@ -228,6 +228,24 @@ describe('handler', function() {
228228
});
229229
});
230230

231+
it('middleware can override result after return next()', function() {
232+
const stub = sinon.stub().returns('foo');
233+
const fixture = lambdaHandler()
234+
.use(async (event, context, next) => {
235+
return next()
236+
.then(() => {
237+
return 'bar';
238+
});
239+
})
240+
.use(stub);
241+
242+
return fixture(testEvent, testContext)
243+
.then(result => {
244+
expect(result).to.equal('bar');
245+
expect(stub.calledOnce).to.be.true;
246+
});
247+
});
248+
231249
it('can do things around next() without affecting result', function() {
232250
const obj = {};
233251
let after = false;
@@ -245,6 +263,20 @@ describe('handler', function() {
245263
});
246264
});
247265

266+
it('can have middleware return next() without affecting result value', function() {
267+
const obj = {};
268+
const fixture = lambdaHandler()
269+
.use((event, context, next) => {
270+
return next();
271+
})
272+
.use(() => obj);
273+
274+
return fixture(testEvent, testContext)
275+
.then(result => {
276+
expect(result).to.equal(obj);
277+
});
278+
});
279+
248280
it('has expected execution order', function() {
249281
const order = [];
250282
const fixture = lambdaHandler()

0 commit comments

Comments
 (0)