generated from tc39/template-for-proposals
-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Description
if you want the first .next(0.1) to work with yield, you can just wrap the generator and just drop the first .next on the floor
const skipFirstYield =
(generator) =>
(...parameters) => {
const iterator = generator(...parameters)
iterator.next()
return iterator
}
const adder = skipFirstYield(function* (total = 0) {
let increment = 1
let request
while (true) {
switch ((request = yield (total += increment))) {
case undefined:
break
case 'done':
return total
default:
increment = Number(request)
}
}
})
let tally = adder()
tally.next(0.1) // argument will be used!
tally.next(0.1)
tally.next(0.1)
let last = tally.next('done')
console.log(last.value) // 1.3 as desiredMetadata
Metadata
Assignees
Labels
No labels