Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/spudnyk/async-wrappers
Various functions for asynchronous operations
https://github.com/spudnyk/async-wrappers
async async-functions debounce javascript promise throttle
Last synced: about 3 hours ago
JSON representation
Various functions for asynchronous operations
- Host: GitHub
- URL: https://github.com/spudnyk/async-wrappers
- Owner: SpudNyk
- License: mit
- Created: 2019-06-06T18:02:16.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T02:00:29.000Z (almost 2 years ago)
- Last Synced: 2024-11-09T21:44:32.585Z (6 days ago)
- Topics: async, async-functions, debounce, javascript, promise, throttle
- Language: TypeScript
- Homepage: https://spudnyk.github.io/async-wrappers
- Size: 2.21 MB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 16
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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
```