1.1.1 • Published 9 years ago
replace-async v1.1.1
replace-async
Like string.replace, but with asynchronous, concurrent replacement.
install
npm install replace-asyncexample
const replace = require('replace-async')
replace('{{foo}} {{bar}}', regex, (done, match, ...etc) {
somethingAsync(match, (err, newValue) => {
done(err, newValue)
})
}, (errors, result) => {
})API
replace(string, pattern, replacement, [options], cb
stringpattern: string or RegExpreplacement: string or functionoptions: objectignoreErrors: boolean, falseBy default, if any replacement functions fail, no result will be produced and replacement cleanup functions will be called. When true, produces a result even if there are errors and does not call cleanup functions.
cb: (errors, result) => {}- errors:
undefined or []errors are placed at the index of their matching replacement (an error on the first replacement will beerrors[0]) - result:
string or undefinedresult of replacement orundefinedif there were any errors
- errors:
replacement function
The arguments are just like string.replace, but begins with a node-style callback. If the asynchronous operation fails, pass done(err), otherwise pass the replacement value done(undefined, newValue).
You can return a function from the replacement function and it will be called for each replacement that has not called its callback yet if any replacements fail. This gives you opportunity to cancel/cleanup unfinished operations.
(done, match) => {
const op = asyncOperation((err, result) => {
done(err, result)
})
return () => op.cancel(done)
}