Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Promise grammar support. #166

Open
Vallista opened this issue Mar 2, 2017 · 7 comments
Open

Promise grammar support. #166

Vallista opened this issue Mar 2, 2017 · 7 comments
Labels

Comments

@Vallista
Copy link

Vallista commented Mar 2, 2017

I thought that I would add it because it was not promise while using it. What do you think?

@dougwilson
Copy link
Contributor

Hi @Vallista I looked into promises, but since this is an event based model vs a callback based one, it isn't clear how to make use of promises in the API. Can you provide an outline (either in text or as a PR) for how promises could be incorporated in the API?

@kilianc
Copy link

kilianc commented Jun 28, 2017

const form = new multiparty.Form()
const [fields, files] = await form.parse(ctx.req)

@jimmywarting
Copy link

jimmywarting commented Mar 17, 2020

Would like to have a async iterator... kinda best of both worlds awaitable + event based (can yield one entry at the time)

const form = new multiparty.Form()

for await (let entry of form.parse(req)) {
  console.log(entry.headers) // [ [key, value], ... ]
  console.log(entry.name) // field name
  console.log(entry.fileName) // filename

  // entry.value is an async iterator also
  for await (let chunk of entry.value) {
    // do something with chunk
  }

  // or
  stream.Readable.from(entry.value).pipe(fs.createWriteStream(entry.filename))
}

but it seems farfetched judging by the design of this lib

@davidmroth
Copy link

davidmroth commented Mar 11, 2022

const [fields, files] = await form.parse(ctx.req)

Maybe something like this?

const parseForm = req =>
  new Promise((resolve, reject) => {
    new multiparty.Form().parse(req, function (err, fields, files) {
      if (err) reject(err)
      resolve([fields, files])
    })
  })

...and then call it like this:

const [fields, files] = await parseForm(req)

@Lyokolux
Copy link

Lyokolux commented May 26, 2022

const [fields, files] = await form.parse(ctx.req)

Maybe something like this?

const parseForm = req =>
  new Promise((resolve, reject) => {
    new multiparty.Form().parse(req, function (err, fields, files) {
      if (err) reject(err)
      resolve([fields, files])
    })
  })

...and then call it like this:

const [fields, files] = await parseForm(req)

Hey 👋

I was having a hard time recieving files using Nuxt 3 and its Nitro server with multiparty.
Thus because nitro is event based and does not wait for the callback.

Your parseForm function solved it 👍

@cherviakovtaskworld
Copy link

Do we still need to manually promisify this in the end of 2023?

@dougwilson
Copy link
Contributor

If someone wants to start a PR to add promises, that would be a big help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants