Skip to content

Instantly share code, notes, and snippets.

@kevinswiber
Last active December 16, 2015 08:29
Show Gist options
  • Save kevinswiber/5406705 to your computer and use it in GitHub Desktop.
Save kevinswiber/5406705 to your computer and use it in GitHub Desktop.
A limited work-in-progress system with Pipeworks.
var pipeworks = require('pipeworks');
var wip = 0;
var limit = 2;
var total = 0;
var check = function(context, next) {
if (wip === limit) {
console.log('I\'ll eat', context.food, 'later...');
setTimeout(check.bind(this, context, next), 2000);
} else {
next(context);
}
};
var mealtime = pipeworks()
.fit(check)
.fit(function(context, next) {
wip++;
next(context);
})
.fit(function(context, next) {
setTimeout(function() {
console.log('I eat', context.food);
next(context);
}, 100);
})
.fit(function(context, next) {
wip--;
next(context);
})
.fit(function(context, next) {
total++;
if (total < foods.length) {
console.log('I\'m still hungry...');
} else {
console.log('Now I`m stuffed!');
}
next(context);
});
var foods = ['burritos', 'tacos', 'nachos', 'enchiladas'];
foods.forEach(function(food) {
mealtime.flow({ food: food });
});
/* Output:
I'll eat nachos later...
I'll eat enchiladas later...
I eat burritos
I'm still hungry...
I eat tacos
I'm still hungry...
<...pause...>
I eat nachos
I'm still hungry...
I eat enchiladas
Now I`m stuffed!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment