Skip to content

viable workaround before this lands? #12

@Pyrolistical

Description

@Pyrolistical

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 desired

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions