1.0.0 • Published 5 years ago

async-task-reconciler v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

async-task-reconciler

coveragenpm version

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

1.0.0

5 years ago

1.0.0-beta.0

5 years ago