Returns a function which will capture and collect all invocation arguments, to process in batches
once ms
consecutive resting time has passed.
Know a more common name for this function? Let me know in this discussion.
const fn = losslessDebounce(console.log, 100)
fn(1)
await sleep(50)
fn(2, 3)
await sleep(100)
// 1, 2, 3
fn(4)
fn(5)
await sleep(100)
// 4, 5
Reduce the number of calls made to an upstream API, by making one call with a payload that contains multiple values, instead of multiple calls with one value each.
const updateProductsById = losslessDebounce((...productIds: string[]): void => {
fetch('https://some-scraper.com/v2/trigger-job-in-the-background', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
ACTOR_NAME: 'UPDATE_PRODUCTS_BY_ID',
productIds,
}),
});
}, 5000);