Skip to content

fix: wrap middleware in promise - #1119

Closed
InsOpDe wants to merge 1 commit into
expressjs:mainfrom
InsOpDe:fix/promisify
Closed

InsOpDe wants to merge 1 commit into
expressjs:mainfrom
InsOpDe:fix/promisify

Conversation

@InsOpDe

@InsOpDe InsOpDe commented Jul 7, 2022

Copy link
Copy Markdown

this is necessary, so that async hooks work

#814
#1046

this is necessary, so that async hooks work
@InsOpDe

InsOpDe commented Jul 7, 2022

Copy link
Copy Markdown
Author

tests all pass.

no new tests have been added

@LinusU

LinusU commented Jul 7, 2022

Copy link
Copy Markdown
Member

Thank you for the PR! Currently there are unfortunately two problems which means we cannot merge it yet:

  • This is a breaking change, since Multer 1.x supports Node.js 0.10 which doesn't have Promise
  • If next throws an error that will be caught by us, and then we will call next again with that error. This is a very common misstake to make when trying to "callbackify" a promise. You can look here for some info on doing it the "correct" way: https://github.com/LinusU/util-callbackify/blob/master/index.js

@InsOpDe

InsOpDe commented Jul 7, 2022

Copy link
Copy Markdown
Author

Hi @LinusU do we have tests which check for your second point?

I wouldn't know how to fix the problem without promises. Should we close that PR then?

@LinusU

LinusU commented Jul 7, 2022

Copy link
Copy Markdown
Member

[...] do we have tests which check for your second point?

Not at the moment, but this would be a great to add!

I wouldn't know how to fix the problem without promises. Should we close that PR then?

One idea could be to only enable this path if typeof Promise === 'function'. I'm not to familiar with the context but I think that this can be solved without wrapping the entire thing in a Promise, instead using the async_hook module to track the context.

There seems to be some documentation here: https://nodejs.org/api/async_context.html

This could then be imported something like:

var asyncHook
try {
  asyncHook = require('async_hook')
} catch (_) {}

@UlisesGascon

Copy link
Copy Markdown
Member

Closing in favour of #1124, which fixes the same async-context loss (#814, #1046) with an AsyncResource and without the double-next problem Linus pointed out. Thanks for raising it back then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants