tc39 / proposal-do-expressions

Proposal for `do` expressions
MIT License
1.12k stars 14 forks source link

idea: `do *` generators #76

Open jedwards1211 opened 1 year ago

jedwards1211 commented 1 year ago

I wrote code like this today:

await Promise.all(
  function* () {
    for (const channel of this.channels) {
      if (!channel.enabled) continue
      const newValue = newValues[channel.tag]
      if (channel.value !== newValue) yield channel.update({value: newValue})
    }
  }.call(this)
)

This is a pretty nice pattern but IIFEs always feel messy. I realized, a do * syntax would make this pattern more elegant:

await Promise.all(
  do * {
    for (const channel of this.channels) {
      if (!channel.enabled) continue
      const newValue = newValues[channel.tag]
      if (channel.value !== newValue) yield channel.update({value: newValue})
    }
  }
)
gioragutt commented 1 year ago

Yes please! I just came here to look for documentation of this feature, and this is exactly what I want to see.

The current syntax for using generators in-place is really cumbersome, and something like do* will make it much easier to use

sirisian commented 2 months ago

This is especially nice when combining multiple generators.

for (const v of do * {
  yield* foo();
  yield* bar();
}) {
  // v
}