12 lines
300 B
JavaScript
12 lines
300 B
JavaScript
export async function replaceAsync(str, regex, asyncFn) {
|
|
const promises = [];
|
|
|
|
str.replace(regex, (match, ...args) => {
|
|
const promise = asyncFn(match, ...args);
|
|
promises.push(promise);
|
|
});
|
|
|
|
const data = await Promise.all(promises);
|
|
return str.replace(regex, () => data.shift());
|
|
}
|