UNPKG

async-wrappers

Version:

A set of wrapper functions to perform debouncing, throttling, retrying etc.

63 lines (50 loc) 1.61 kB
# async-wrappers Implementations of debounce, throttle, retry and more targeted to promises/async/await. ## Key Features 1. call argument aggregation - all arguments from calls to the wrapper can be combined for the call to the wrapped function, using a supplied argument reducer function. See the api documentation for supplied reducers. 2. call return values - calls to the wrapper will return a promise to wrapped function's result. 3. delay times of 0 are still asynchronous and will resolve on the next runtime loop. ## Documentation See the [API](https://spudnyk.github.io/async-wrappers/api/). ## Example ```javascript const { debounce, combineArguments } = require('async-wrappers'); const data = { 1: 'data 1', 2: 'data 2' //... }; // this could be fetching from a database or webservice // debounce supports async functions or returned promises const getByIds = ids => { console.log(`Getting: ${JSON.stringify(ids)}`); const keyed = {}; // this could be a server request for (const id of ids) { keyed[id] = data[id]; } return keyed; }; // data loader equivalent const load = debounce(getByIds, 0, { reducer: combineArguments }); const loadData = async id => { const data = await load(id); return data[id]; }; const main = async () => { const foo = loadData(2); const bar = loadData(1); const baz = loadData(3); console.log(`Foo: ${await foo}`); console.log(`Bar: ${await bar}`); console.log(`Baz: ${await baz}`); }; main(); // Outputs: // Getting: [2,1,3] // Foo: data 2 // Bar: data 1 // Baz: undefined ```