2.1.1 • Published 4 years ago

async-wrappers v2.1.1

Weekly downloads
19
License
MIT
Repository
github
Last release
4 years ago

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.

Example

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
2.1.1

4 years ago

2.0.3

4 years ago

2.1.0

4 years ago

2.0.2

4 years ago

2.0.1

4 years ago

2.0.0

4 years ago