Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] allow async event handler functions #697

Open
andrisi opened this issue Jul 20, 2021 · 3 comments
Open

[FEATURE] allow async event handler functions #697

andrisi opened this issue Jul 20, 2021 · 3 comments
Assignees

Comments

@andrisi
Copy link

andrisi commented Jul 20, 2021

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, ...

@andrisi
Copy link
Author

andrisi commented Feb 15, 2022

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?

@pigpudle
Copy link

@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', () => {
      
    });

@andrisi
Copy link
Author

andrisi commented Jun 23, 2023

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants