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

Allow async callbacks in reply #1757

Closed
robertcepa opened this issue Nov 6, 2022 · 1 comment · Fixed by #1758
Closed

Allow async callbacks in reply #1757

robertcepa opened this issue Nov 6, 2022 · 1 comment · Fixed by #1758
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@robertcepa
Copy link

This would solve...

I am using a library that proxies undici in an unintended way, changing the type of opts.body to AsyncGenerator. I would like to read the generator and reply with the result, but it's not possible because reply callback isn't awaited. This was discussed more in-depth here #1756.

This capability would also allow awaiting other async functions in the callback

The implementation should look like...

In my specific use-case, I'd be able to read AsyncGenerator and return the body string:

.reply(200, async (opts) => {
  const buffers = [];
  for await (const data of opts.body) {
    buffers.push(data);
  }
  const body = Buffer.concat(buffers).toString('utf8');
    
  return body;
});
@robertcepa robertcepa added the enhancement New feature or request label Nov 6, 2022
@mcollina
Copy link
Member

mcollina commented Nov 6, 2022

Thanks for reporting! Would you like to send a Pull Request to address this issue? Remember to add unit tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants