15 lines
432 B
TypeScript
15 lines
432 B
TypeScript
|
export async function replaceAsync(
|
||
|
str: string,
|
||
|
regex: RegExp,
|
||
|
asyncFn: (match: string, ...args: any[]) => Promise<string>
|
||
|
) {
|
||
|
const promises: Promise<string>[] = [];
|
||
|
str.replace(regex, (match: string, ...args: any[]): string => {
|
||
|
const promise = asyncFn(match, ...args);
|
||
|
promises.push(promise);
|
||
|
return "";
|
||
|
});
|
||
|
const data = await Promise.all(promises);
|
||
|
return str.replace(regex, () => data.shift()!);
|
||
|
}
|