-
Notifications
You must be signed in to change notification settings - Fork 0
async.Function.pendingFn
github-actions[bot] edited this page Jun 8, 2026
·
2 revisions
@zenstone/ts-utils / async / pendingFn
pendingFn<
T,Args>(scope,fn): (...args) =>Promise<Awaited<T>>
Defined in: src/async/pending.ts:93
感知 pending 状态的 inflight 去重
fn 的第一个参数为 PendingFnParams,包含 scope 和 getPendingCount, 允许根据 pending 状态做自定义处理(如短路检查、日志等)。 返回的函数剥掉 PendingFnParams,只暴露业务参数。
T
Args extends unknown[]
string | ((...args) => string)
静态 scope 字符串,或基于参数动态生成 scope 的函数
PendingCallbackFn<T, Args>
接收 PendingFnParams 的回调函数
只保留业务参数的包装函数
(...args) => Promise<Awaited<T>>
const mountScript = pendingFn(
(url: string) => `script:${url}`,
({ scope, getPendingCount }, url: string) => {
// 短路:已挂载
const existing = document.getElementById(scope);
if (existing) return { element: existing };
// 实际挂载,执行期间 getPendingCount() 可获取等待数
return loadScript(url);
},
);
// 并发调用,只执行一次挂载
await Promise.all([mountScript('/lib.js'), mountScript('/lib.js')]);