multiparty icon indicating copy to clipboard operation
multiparty copied to clipboard

Promise grammar support.

Open Vallista opened this issue 8 years ago • 7 comments

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

Vallista avatar Mar 02 '17 13:03 Vallista

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?

dougwilson avatar Mar 27 '17 04:03 dougwilson

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

kilianc avatar Jun 28 '17 23:06 kilianc

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

jimmywarting avatar Mar 17 '20 12:03 jimmywarting

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)

davidmroth avatar Mar 11 '22 20:03 davidmroth

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 👍

Lyokolux avatar May 26 '22 05:05 Lyokolux

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

cherviakovtaskworld avatar Dec 06 '23 05:12 cherviakovtaskworld

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

dougwilson avatar Dec 06 '23 05:12 dougwilson