Skip to content

Commit

Permalink
change the passing method for sync objects.
Browse files Browse the repository at this point in the history
Not support node 4.x anymore.
  • Loading branch information
unixzii committed Apr 15, 2017
1 parent 24e4a52 commit 3a191ee
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 19 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
language: node_js
node_js:
- '7'
- '6'
- '4'
- '6'
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,21 @@ pSeq(op1, op2, op3)
});
```

When the `Promise` of a task resolved, **p-seq** passes its result to next function. So there will be only one task is running at one time.
When the `Promise` of a task resolved, **p-seq** passes its result to next task generator. So there will be only one task is running at one time.

## API

`pSeq(tasks)`
### Arguments
* tasks: An array of tasks, element type is `Promise`, `Function` or `GeneratorFunction`.
* tasks: An array of task generators, element type is `Promise`, `Function` or `GeneratorFunction`.

### Return Value
A `Promise` represented the last task.

### Discussion
When a task is `Promise`, **p-seq** just pass its result to next task. When a task is function or generator function, **p-seq** call it when last task is resolved, and pass the result of last task to it, the function must return a `Promise`. Since the generator is run via [co](https://www.npmjs.com/package/co), it produces a `Promise` intrinsically.
When a task generator is `Promise`, **p-seq** just pass its result to next task. When it is a function or generator function, **p-seq** call it when last task is resolved, and pass the result of last task to it, the function should return a `Promise`, otherwise **p-seq** just pass the return value through. Since the generator is run via [co](https://www.npmjs.com/package/co), it produces a `Promise` intrinsically.

If a task generator's type is none of above, **p-seq** just pass itself through the chain, but we strongly recommend you to avoid doing that.

**There is also a more convenient way to pass tasks:**

Expand Down
18 changes: 6 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,9 @@ function pSeq(tasks) {
_tasks = tasks;
} else {
const args = Array.prototype.slice.call(arguments);

for (const key in args) {
if (Object.prototype.hasOwnProperty.call(args, key)) {
_tasks.push(args[key]);
}
}
args.forEach(arg => {
_tasks.push(arg);
});
}

return _tasks.reduce((promise, task) => {
Expand All @@ -28,16 +25,13 @@ function pSeq(tasks) {
next = task;
} else if (isFn(task)) {
next = task(value);
if (!pIsPromise(next)) {
return;
}
} else if (isGeneratorFn(task)) {
next = co(task(value));
} else {
next = task;
}

if (pIsPromise(next)) {
return next;
}
return next;
});
}, Promise.resolve());
}
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"name": "p-seq",
"version": "1.0.1",
"version": "1.1.0",
"description": "Run async tasks sequently",
"main": "index.js",
"scripts": {
"test": "xo && mocha",
"coverage": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
"coverage": "istanbul cover _mocha --report lcovonly -- -R spec && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage"
},
"files": [
"index.js"
],
"engines": {
"node": ">6"
},
"repository": {
"type": "git",
"url": "git+https://github.com/unixzii/p-seq.git"
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ function * asyncWork4(filename) {

function * asyncWork5(content) {
yield sleep(100);
if (!content) {
return 0;
}
return content.length;
}

Expand Down Expand Up @@ -91,6 +94,13 @@ describe('Two way to pass arguments', () => {
});
});

describe('Tasks that are not async', () => {
it('should pass its result or itself through the chain', () => {
return expect(pSeq([asyncWork3, asyncWork4, 'Hello', asyncWork5, count => count + 1]))
.to.eventually.equal(6, 'result is wrong');
});
});

describe('Handle error in a middle task', () => {
it('should stop when error occurs', () => {
return expect(pSeq(asyncWork3, asyncWork4, mockErrorProducer, asyncWork5))
Expand Down

0 comments on commit 3a191ee

Please sign in to comment.