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

Async custom processors #204

Open
johnnyreilly opened this issue Nov 25, 2024 · 1 comment
Open

Async custom processors #204

johnnyreilly opened this issue Nov 25, 2024 · 1 comment

Comments

@johnnyreilly
Copy link
Contributor

johnnyreilly commented Nov 25, 2024

Hello!

I've been reading about custom processors:

Using custom processors

For advanced usage where complex processing is needed it's possible to use a callback that will receive content as an argument and should return it processed.

const results = await replaceInFile({
  files: 'path/to/files/*.html',
  processor: (input) => input.replace(/foo/g, 'bar'),
})

The custom processor will receive the path of the file being processed as a second parameter:

const results = await replaceInFile({
  files: 'path/to/files/*.html',
  processor: (input, file) => input.replace(/foo/g, file),
})

Custom processors are great, and very powerful!

However, imagine the scenario where you want to drive the output of a processor from an asynchronous API, based upon the contents of the text file. At present, you can't use custom processors for that scenario as they are synchronous.

What do you think about having an asynchronous custom processors API as well? Something like this say:

const results = await replaceInFile({
  files: 'path/to/files/*.html',
  processorAsync: async (input, file) => { 
    const asyncResult = await doAsyncOperation(input, file);
    return input.replace(/foo/g, asyncResult) 
  },
})
@adamreisnz
Copy link
Owner

Sure, that seems sensible. It would require some changes in the architecture of how processors are currently handled, but not impossible.

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

2 participants