m4nuC / async-busboy

Promise based multipart form parser for KoaJS
MIT License
167 stars 58 forks source link

How can you do onFile with async? #41

Closed katlimruiz closed 3 years ago

katlimruiz commented 4 years ago

Hi

So I have a question I want to do an async/await pattern with the onFile custom handler.

I tried several ways and I dont' think I found one that works out.

I know this may be very simple, but I couldn't figure it out myself.

fuguohong commented 4 years ago
async (ctx) => {
  await new Promise((resolve, reject) => {
    asyncBusboy(ctx.req, {
      onFile: async (fieldname, file, filename, encoding, mimetype) => {
        // ...........
        await saveFile();
        resolve();
      },
    });
  });
  // continue...
};
youbek commented 3 years ago

Quick question. What if we have multiple uploads? Does promise resolve when the first file has been uploaded?

youbek commented 3 years ago

Currently, I'm using this approach. Is it redundant to store pending promises in the array?

  const pendingUploads: Promise<any>[] = [];

  const { files, fields } = await asyncBusboy(req, {
    onFile: async (fieldname, file, filename, encoding, mimetype) => {
      pendingUploads.push(s3.upload(filename, mimetype, file));
    },
  });

  await Promise.all(pendingUploads);

  console.log(`All Uploads Are Done!`);

  res.send(200);