-
Notifications
You must be signed in to change notification settings - Fork 7
/
async-replace.ts
32 lines (28 loc) · 1.08 KB
/
async-replace.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
const DO_NOTHING = () => {};
// function makePromiseSeq(promiseFn: AsyncFn) {
// let top = Promise.resolve();
// return (match: string, ...args: string[]) => {
// top = top
// .catch(DO_NOTHING)
// .then(() => promiseFn(match, ...args));
// return top;
// };
// }
export async function replace(str: string, regex: RegExp, asyncFn: AsyncFn, notifyCb = DO_NOTHING) {
const promises: Promise<string>[] = [];
str.replace(regex, (match, ...args) => {
const promise: Promise<string> = asyncFn(match, ...args);
promises.push(promise);
promise
.then(notifyCb)
.catch(DO_NOTHING);
return match;
});
const data = await Promise.all(promises);
return str.replace(regex, () => data.shift() as string);
}
// function replaceSeq(str: string, regex: RegExp, asyncFn: AsyncFn, notifyCb = DO_NOTHING) {
// const asyncFnSeq = makePromiseSeq(asyncFn);
// return replace(str, regex, asyncFnSeq, notifyCb);
// }
type AsyncFn = (match: string, ...groups: string[]) => Promise<string>;