C2FO / fast-csv

CSV parser and formatter for node

Home Page:http://c2fo.github.io/fast-csv

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[FEATURE] allow async event handler functions

andrisi opened this issue · comments

Is your feature request related to a problem? Please describe.
Using async event handlers. Most of my code uses sync function and need to be called with await.

Describe the solution you'd like
If an event handler returns a promise, wait on it.

Describe alternatives you've considered
Calling async functions from sync code, ...

Any update on this @doug-martin or @dustinsmith1024 - seems like an easy addition but something that would help using standard await/async construncts in modern js. Thanks a lot. Or maybe not that easy because you're not using async functions where you'd need to await event handlers?

@andrisi You can pause the stream while waiting for async operations, like this:

  const csvStream = fs.createReadStream('file.csv')
    .pipe(csv.parse({ headers: true }))
    .on('data', async (row) => {
      try {
        csvStream.pause();
        await doSomethingAsync(row);
      } finally {
        csvStream.resume();
      }
    })
    .on('end', () => {
      
    });

Thanks @pigpudle good workaround, will use it! Handling async event handlers properly on the other hand - with all events -would be cleaner and in line with what you'd expect when you add an async event handler. It's easy to detect that the result of the event handler function is a promise. Or it's handled outside of your code? I got to learn more about streams...