multiparty
multiparty copied to clipboard
Promise grammar support.
I thought that I would add it because it was not promise while using it. What do you think?
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?
const form = new multiparty.Form()
const [fields, files] = await form.parse(ctx.req)
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
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)
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 👍
Do we still need to manually promisify this in the end of 2023?
If someone wants to start a PR to add promises, that would be a big help :)