mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1type BundledFn<Args extends readonly unknown[], Res> = ( 2 ...args: Args 3) => Promise<Res> 4 5/** 6 * A helper which ensures that multiple calls to an async function 7 * only produces one in-flight request at a time. 8 */ 9export function bundleAsync<Args extends readonly unknown[], Res>( 10 fn: BundledFn<Args, Res>, 11): BundledFn<Args, Res> { 12 let promise: Promise<Res> | undefined 13 return async (...args) => { 14 if (promise) { 15 return promise 16 } 17 promise = fn(...args) 18 try { 19 return await promise 20 } finally { 21 promise = undefined 22 } 23 } 24}