Created
March 2, 2018 07:57
-
-
Save rzkhosroshahi/dc3504417172185cc2bfc025d73e214d to your computer and use it in GitHub Desktop.
array consumer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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