From cd5a2748ffe5ef1be55789bf77c13f94dcd50829 Mon Sep 17 00:00:00 2001 From: Mustafa Dokumaci Date: Thu, 27 Oct 2016 10:37:46 +0200 Subject: [PATCH] better readme --- README.md | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) diff --git a/README.md b/README.md index 5f59011..0364cb6 100644 --- a/README.md +++ b/README.md @@ -14,3 +14,60 @@ npm install concurrent-task --save ``` ## Usage + +Below is an example of 5 tasks with 2 steps to execute for each. + +```js + const concurrent('concurrent-task') + + // create a task runner with concurrency limit 2 + const runner = concurrent(2) + + runner.set({ + steps: { + step1: { + timeout: 1000, + tryCount: 2, + run (data, resolve, reject) { + return clearTimeout.bind(null, setTimeout(() => { + return data.success.compute() ? resolve() : reject(new Error('some error')) + }, data.seconds * 1000)) + } + }, + step2: { + timeout: 500, + tryCount: 3, + run (data, resolve, reject) { + return clearTimeout.bind(null, setTimeout(() => { + return data.success.compute() ? resolve() : reject(new Error('some error')) + }, data.seconds * 1000)) + } + } + }, + tasks: { + task1: { seconds: 0.3, success: true }, + task2: { seconds: 0.7, success: true }, + task3: { seconds: 0.2, success: false }, + task4: { seconds: 1.2, success: false }, + task5: { seconds: 0.3, success: true } + } + }) + + runner + .on('error', (key, error) => { + // one of tasks had an error on one of steps + // key is key of task + }) + .on('task-done', key => { + // one of tasks completed all steps with no error + }) + .on('complete', () => { + // all the tasks completed all steps + // if any of them had errors + // tryCounts are exhausted + + // log runner status + console.log(runner.status()) + }) + .run() +```