Run replace on a string with an asynchronous function (promises).
Usage
async-replace-es6 has the same api as String.prototype.replace
. Only the callback is an async function.
This is usefull if you need the matched part of a string before running an asynchronous operation.
import { replace } from 'async-replace-es6';
const { replace } = require('async-replace-es6');
const replaced = await replace(originalString, /someregex/g, asyncReplacer);
Or
replace(originalString, /someregex/, asyncReplacer)
.then(replaced => console.log(replaced));
Example of asynchronous replacer :
async function asyncReplace(match, group1, group2, ...) {
const response = await fetch(match);
const json = await reponse.json();
return json['title'];
}
function asyncReplacer(match, ...groups) {
return new Promise((resolve, reject) => {
fs.readFile(match, (err, data) => {
if (err) {
return reject(err);
}
resolve(data.toString());
});
});
}
If your promises need to be done Sequentially here is a solution:
const { replaceSeq } = require('async-replace-es6');
replaceSeq(originalString, /someregex/g, asyncReplacer)
.then(replaced => console.log(replaced));