Promise support for form.parse()
See original GitHub issueI am thinking that it could be nice to make form.parse()
promisified: if no callback is provided, it will return a promise.
This could allow us to rewrite the following code:
form.parse(req, (err, fields, files) => {
if (err) {
// ... handle error
return
}
// do something with fields and files
})
Into something like this:
try {
const [fields, files] = await form.parse(req)
// ... do something with fields and files
} catch (e) {
// ... handle error
}
Is this something that has been considered already?
I had a quick look at the code and it seems that parse
currently returns this
(i suppose to allow method chaining). This seems to be a potential blocker for this feature (as it will require a breaking change), but as an alternative, it could be possible to create a new method called parsePromise
or something similar, which can act as a wrapper for the original parse method…
The main advantages I see in supporting this syntax are the following:
- Unified error handling (one catch to rule them all)
- Integrates well with async handlers where there might be other async logic before or after the form parsing
- Fewer callbacks nesting
Thoughts?
If this idea resonates with the maintainers and the community, I’d be happy to find some time to submit a PR…
Issue Analytics
- State:
- Created 3 years ago
- Reactions:21
- Comments:17 (10 by maintainers)
Should form.parse()
No the issue is still open We are waiting for someone to make a PR