Skip to content

Instantly share code, notes, and snippets.

@rzkhosroshahi
Created March 2, 2018 07:57
Show Gist options
  • Save rzkhosroshahi/dc3504417172185cc2bfc025d73e214d to your computer and use it in GitHub Desktop.
Save rzkhosroshahi/dc3504417172185cc2bfc025d73e214d to your computer and use it in GitHub Desktop.
array consumer
const coroutine = require('./coroutine');
var arr = [1,2,3,4,5,6,7,8,9,10];
function reader(target, i) {
target.next(i);
};
const arrConsumer = coroutine(function* (target) {
try {
let index = yield;
while (index < arr.length) {
target.next(arr[index])
index++;
}
} finally {
target.return();
console.log('done')
}
});
const printer = coroutine(function* () {
while (true) {
console.log(yield);
}
});
reader(arrConsumer(printer()), 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment