question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Promise support for form.parse()

See original GitHub issue

I 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:open
  • Created 3 years ago
  • Reactions:21
  • Comments:17 (10 by maintainers)

github_iconTop GitHub Comments

34reactions
GrosSacASaccommented, Aug 11, 2021

Should form.parse()

  • awlays return a promise (vote with 👍 )
  • or be callback based if a function is passed to it and else return a promise (vote with ❤️ )
  • other 😕 (explain in comment)
2reactions
GrosSacASaccommented, Jul 10, 2021

No the issue is still open We are waiting for someone to make a PR

Read more comments on GitHub >

github_iconTop Results From Across the Web

Developers - Promise support for form.parse() -
I am thinking that it could be nice to make form.parse() promisified: if no callback is provided, it will return a promise.
Read more >
nodejs formidable async issue
parse() in a Promise , formidable does not (yet) support promises natively. The following will trigger a TypeError: is not a function error ......
Read more >
Using promises - JavaScript - MDN Web Docs
A Promise is an object representing the eventual completion or failure of ... Here's the magic: the then() function returns a new promise, ......
Read more >
Differences between formidable callbacks, events and ...
form.parse(request); form.on('file', (name, file) => saveFile(file)); ... We use saveFiles() to return a promise object named filePaths .
Read more >
How to use IncomingForm function in formidable - Javascript
keepExtensions = true; var parse = Promise.promisify(form.parse, form); var ... res, next) { var msg = '' var num = random() var form...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found