javascript - Composing generator producer of values -
how can compose producer generator gets values externally generator? if producer
yields value within generator works, when send value next
undefined
. there way this?
function* producer() { while (true) { yield // want send values here } } function* take(n, gen) { var = 0 (var x of gen) { if (i <= n) { yield x // want value sent producer += 1 } else { break } } } var prod = producer() var res = take(5, prod) // how can send values producer // , make result yield values? res.next() // init console.log(res.next(1)) // {value: undefined, done: false} console.log(res.next(2)) // {value: undefined, done: false} // ... // want // {value: 1, done: false} // {value: 2, done: false} // ...
to value sent next()
, should use result returned yield. not quite sure end goal is, here modified example:
let producer = function*() { var nextresult = undefined; while(true) { nextresult = yield nextresult; console.log("produce " + nextresult); } } let take = function*(number, items) { var count = 0; var item = undefined; while (count <= number) { items.next(item); item = yield item; console.log("take " + item); count += 1; } } var res = take(2, producer()) res.next() // init console.log('result ' + res.next(3).value) console.log('result ' + res.next(2).value)
this outputs:
take 3 produce 3 result 3 take 2 produce 2 result 2
Comments
Post a Comment