1

I want to process the operation in parallel as soon as files are being received at server in non-blocking way.

I have tried this:

(async () => {
      await io.setMaxListeners(0)
      await io.on('connection', async (client) => {
        await fse.copy(__dirname + /files/ + event.file.id, `${fileinfo}${meta.name}`);
        console.log("File copied");

        const thumbDirExists = await fse.pathExists(thumbDir);
        thumbDirExists === false && await fse.ensureDir(thumbDir)
        console.log("Thumb Dir created");

        let readyFile = `${fileinfo}${meta.name}`;
        let resizeFile = `${thumbDir}${meta.name}`;

        await createThumb(readyFile, resizeFile).then(async imageBuffer => {
          console.log("Resized!")
          await client.emit('image', { image: true, buffer: imageBuffer.toString('base64') });
          console.log("Image sent")
        })
      })
    })();

Expected result:

File copied
Thumb Dir created
Resized
Image sent
File copied
Thumb Dir created
Resized
Image sent
File copied
Thumb Dir created
Resized
Image sent

Actual result:

File copied
File copied
File copied
Thumb Dir created
Thumb Dir created
Thumb Dir created
Resized
Image sent
Resized
Image sent
Resized
Image sent

It seems like the actual result is first copying all the files in blocking way then creating thumb directory in blocking way.

Edit: The duplicate answer does not address my problem. My question is about async/await operation in parallel and non-blocking way. I don't want to use any third party library like promisfy.

JKhan
  • 1,157
  • 4
  • 14
  • 23
  • I think this is actually running in parallel and thats why all files copy console are coming at once. Had they been in series, it'd be like expected result. It's just that the execution of file copy is way faster than the subsequent one. – binariedMe Apr 22 '19 at 07:50
  • @binariedMe I don't think so because thumb dir is not created unless all files are being copied. – JKhan Apr 22 '19 at 08:00
  • 1
    A very simple way to verify that would be to use dummy setTimeout with consoles to check if they are being executed in parallel or not. As per my understanding it is running in parallel otherwise how come we have multiple files copied? if it were in series, everything of one file will be done in one go(copy, mkdir, resize) – binariedMe Apr 22 '19 at 08:05
  • You have a misunderstanding regarding how this works. 'Actual result' already works in parallel. What you're suggesting in 'Expected result' is how it would work in series. FWIW, the code uses antipatterns. You cannot await `io.on(...` because it doesn't return a promise. You shouldn't use `async` as callbacks where returned promise is ignored, this results in poor control flow and error handling. Callback APIs should be promisified to make it right, as the dupe suggests. Async iterators can be used for callbacks that are triggered multiple times, e.g. https://github.com/sindresorhus/p-event – Estus Flask Apr 22 '19 at 09:01
  • @estus can you pls create a new answer with a working example by using p-event? And also how it can be improved without p-event library. – JKhan Apr 22 '19 at 09:14
  • I cannot answer because you didn't elaborate regarding parallel/serial. It's already parallel. There's no blocking. It's unclear why you decided there's a problem. – Estus Flask Apr 22 '19 at 09:29
  • Because I thought that actual result is executed in series rather than parallel. I am still trying to learn async/await. Since you said that my code is anti pattern, I am interested to see improved answer. – JKhan Apr 22 '19 at 09:34
  • 1
    @TedKhi You can leave it as is, it already works. If you will need to exit the app on error, this possibly be a problem that needs a solution. As of now, don't forget `try..catch` inside a callback. Don't await things like `await io.on` and `await io.setMaxListeners(0)` that don't return a promsie, this doesn't serve a good purpose. – Estus Flask Apr 22 '19 at 09:40

0 Answers0