Rewrite dispatch() to benefit from async/await (less error prone)#64
Open
punkeel wants to merge 1 commit intojrf0110:masterfrom
Open
Rewrite dispatch() to benefit from async/await (less error prone)#64punkeel wants to merge 1 commit intojrf0110:masterfrom
punkeel wants to merge 1 commit intojrf0110:masterfrom
Conversation
- Make dispatch() an async function, and await every async function call. By removing Promise.xxx, we reduce the chance that an async function is called without (a)waiting for it to end, which in turn prevents uncaught exceptions. - Simplify code to let errors propagate upwards, without catching them. - Use 'dispatch.bind' in the recursion to remove a function from the stack trace. This binding trick was done in Koa: koajs/compose@13768ff To support this, we move the params-reset to the end of dispatch(). No tests were harmed in the making of this change.
Owner
|
Thanks for this! I've always disliked how inscrutable this function was. I'm going to play around with your change and will likely publish to a release candidate flag for testing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Make dispatch() an async function, and await every async function call.
By removing Promise.xxx, we reduce the chance that an async function
is called without (a)waiting for it to end, which in turn prevents
uncaught exceptions.
Simplify code to let errors propagate upwards, without catching them.
Use 'dispatch.bind' in the recursion to remove a function from the
stack trace. This binding trick was done in Koa:
koajs/compose@13768ff
To support this, we move the params-reset to the end of dispatch().
No tests were harmed in the making of this change.
I am opening this PR because I suspect some exceptions aren't caught by
dispatch()and instead cause uncaught exceptions. I do not have a simple example of this happening, but I suspect it happens here:8track/src/Router.ts
Line 226 in cb4a121
route.handleris an async function, 8track does not await it, and it leaks. Thetry-catchdoesn't see it either, as it's happening in the async world.... or maybeif (p) return p.then(() => ctx.response), we don't attach a.then(fn, fn)nor.catch(fn)...