serviejs / throwback

An asynchronous middleware pattern

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

throw when a promise isn't returned?

matthewmueller opened this issue · comments

not sure how you feel about this (or what would need to change), but it's pretty easy to forget to return next in:

function (ctx, next) {
   return next().then(v => { ... })
}

what do you think about throwing if undefined is returned?

unfortunately, right now not returning a promise silently causes the control flow to change.

Sounds great. I can't see any real caveats to enabling this 👍

The code change required would be: https://github.com/blakeembrey/throwback/blob/master/src/index.ts#L38-L43. Something like const res = fn(); if (res === undefined) { throw }; return resolve(res).

The only unfortunate thing is that it doesn't help once you switch to async/await (which would always return a promise) or your middleware legitimately returns undefined (but we can warn in the README to wrap it in a promise).

Added with a "debug mode" in 12d80cf to use a slower code path in development. The next release is a pretty big code change but should make things simpler, easier and faster to work with (especially next(ctx) support for things like HTTP request retry support in popsicle).