async-task-reconciler
v1.0.0
Published
[![coverage](https://img.shields.io/codecov/c/github/LPegasus/async-task-reconciler.svg?style=flat-square)](https://codecov.io/gh/LPegasus/async-task-reconciler)[![npm version](https://badge.fury.io/js/async-task-reconciler.svg)](https://www.npmjs.com/pac
Downloads
4
Readme
async-task-reconciler
Use to combine async task, cache tasks' results.
Sample
import { AsyncTaskReconciler } from 'async-task-reconciler';
const reconciler = new AsyncTaskReconciler();
// defaultOptions use no cache.
// But if a task with key at pending status,
// before the task resolved, some other tasks
// with the same key come, reconciler won't execute
// these tasks. When the first task fulfilled, all
// the others with the same key will be resolved.
// imagine provide an api which is a query search,
// and request to DB or other backend costs a while.
router.get('/detail/:id', async (req, res) => {
const key = req.params.id;
try {
const queryResult = await reconciler.addTask(() => {
return service.queryDetail({ id: key });
}, key);
res.json(queryResult);
} catch(e) {
// Handle error
}
});
API
ctorOptions
export interface AsyncTaskReconcilerOptions {
cache?:
| boolean
| {
strategy: 'FIFO' | 'LRU'; // clear cache strategy
size: number; // cache size
};
concurrent?: number; // parallel count
}
.addTask
<T>(task: () => Promise<T>, key?: string) => Promise<T>
fn
async task to execute
key
task identifier
.clearCache
() => void
delete caches