Skip to content

Instantly share code, notes, and snippets.

@getify
Last active January 1, 2016 22:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save getify/8211148 to your computer and use it in GitHub Desktop.
Save getify/8211148 to your computer and use it in GitHub Desktop.
Illustrating asynquence "Iterable Sequences"
function step(num) {
return num * 2;
}
// set up an iterable sequence
var isq = ASQ.iterable()
.then(step)
.then(step)
.then(step);
// synchronously iterate the sequence
for (var seed = 3, ret;
// call `next(..)` on the iterable sequence
// to advance it one iteration
(ret = isq.next(seed)) && !ret.done;
) {
seed = ret.value;
console.log(seed);
}
// 6
// 12
// 24
function step(num) {
return num * 2;
}
function iterate(msg) {
// call `next(..)` on the iterable sequence
// to advance it one iteration
var ret = isq.next(msg);
if (!ret.done) {
return ret.value;
}
else {
// break the iteration loop
throw "Iteration complete.";
}
}
// set up an iterable sequence
var isq = ASQ.iterable()
.then(step)
.then(step)
.then(step)
.then(function(){
throw "Should not get here!";
})
.or(function(msg){
console.log(msg); // Too big!
});
// set up an async loop sequence controller
loop = ASQ()
.or(function(msg){
console.log(msg); // Iteration complete.
console.log(seed); // 24
});
// asynchronously iterate the sequence
(function next(msg){
if (msg > 15) {
// throw an error into the iterable
// sequence
sq.throw("Too big!");
}
// store seed so we can check it at the end
seed = msg;
loop
// wait before proceeding with the iteration
.then(function(done){
setTimeout(done,50);
})
.val(
// inject `msg` into the sequence's
// message stream
msg,
// process the current iteration
iterate,
// start up the next loop iteration
next
);
})(3); // start out msg/step as `3`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment