Const
基于 scope 的 inflight 去重(全局实例)
同一 scope 下并发调用只执行一次 fn,所有 callers 共享同一个 Promise 结果。 执行完毕后 scope 释放,下一次调用开启新的执行周期。
全局实例注意:静态 scope 必须在整个应用内唯一。HMR 场景下请改用 createPending 创建模块级独立实例。
静态 scope 字符串,或基于参数动态生成 scope 的函数
要包装的函数(同步或异步)
包装后的去重函数
const fetchConfig = pending('config', () => fetch('/api/config').then(r => r.json()));// 并发调用 3 次,只执行 1 次 fetchconst [a, b, c] = await Promise.all([fetchConfig(), fetchConfig(), fetchConfig()]); Copy
const fetchConfig = pending('config', () => fetch('/api/config').then(r => r.json()));// 并发调用 3 次,只执行 1 次 fetchconst [a, b, c] = await Promise.all([fetchConfig(), fetchConfig(), fetchConfig()]);
const fetchUser = pending( (id: string) => `user:${id}`, (id: string) => fetch(`/api/user/${id}`).then(r => r.json()),);await Promise.all([fetchUser('1'), fetchUser('1'), fetchUser('2')]); Copy
const fetchUser = pending( (id: string) => `user:${id}`, (id: string) => fetch(`/api/user/${id}`).then(r => r.json()),);await Promise.all([fetchUser('1'), fetchUser('1'), fetchUser('2')]);
基于 scope 的 inflight 去重(全局实例)
同一 scope 下并发调用只执行一次 fn,所有 callers 共享同一个 Promise 结果。 执行完毕后 scope 释放,下一次调用开启新的执行周期。
全局实例注意:静态 scope 必须在整个应用内唯一。HMR 场景下请改用 createPending 创建模块级独立实例。