Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add, test/{end,start}.js; Update, lib/index.js #5

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,31 @@ var checkCache = stewardess(
.bind();
```

### Call `next('restart')` to restart the chain

```javascript
stewardess(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make the example simpler and more straightforward. We don't want people getting bogged down in grokking a ternary

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

k, gotcha.

function first(options, next) {
return next(options.idex > 10 ? 'skip' : null);
},
function second(options, next) {
options.arr.push(idex);
return next();
},
function third(options, next) {
return next(options.idex > 10 ? null : 'restart');
}
)
.after(function(options) {
options.idex++;
})
.done(function(options) {
process.stdout.write('arr: ');
console.log(options.arr);
})
.run({arr: [], idex: 0});
```

### Create plugins to repeat setup

```javascript
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,13 @@ Stewardess.prototype.run = function() {

// push the callback onto the arguments
args.push(function(err) {
if (err && [ 'break', 'skip', 'previous', 'repeat' ].indexOf(err) === -1) return error(err);
if (err && [ 'restart', 'break', 'skip', 'previous', 'repeat' ].indexOf(err) === -1) return error(err);
after(fnName);
if (err === 'break') return done();
if (err === 'skip') queuePos += 1;
if (err === 'repeat') queuePos -= 1;
if (err === 'previous') queuePos = Math.max(queuePos - 2, 0);
if (err === 'restart') queuePos = 0;
nextTick(next);
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "stewardess",
"version": "0.2.5",
"version": "0.2.6",
"description": "serial async flow control",
"main": "index.js",
"scripts": {
Expand Down
23 changes: 23 additions & 0 deletions test/begin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";
var stewardess = require('../index');

stewardess(
function first(options, next) {
return next(options.idex > 10 ? 'skip' : null);
},
function second(options, next) {
options.arr.push(options.idex);
return next();
},
function third(options, next) {
return next(options.idex > 10 ? null : 'restart');
}
)
.after(function(options) {
options.idex++;
})
.done(function(options) {
process.stdout.write('arr: ');
console.log(options.arr);
})
.run({arr: [], idex: 0});