2

The following code uses an async iterator to deliver "frames".

But this approach leaves an unused variable (_).

If this is a valid approach, why was while await (a hypothetical feature) not added when for await...of was?

while await would enable the omission of this ignored variable.

const raf = () => new Promise(resolve => requestAnimationFrame(resolve))
const frames = {
  async *[Symbol.asyncIterator]() {
    while (true) yield raf()
  }
}
function createGameLoop(store) {
  async function start() {
    for await (let _ of frames) 
      render(store)
  }
  return { start }
}
5
  • "why was while await not added" I can't seem to find a proposal for this. If there is a proposal, it will be likely find why there - it might still be in progress, or rejected with a reason. If there isn't a proposal...well, there you go - it's why it wasn't added. Commented Apr 15, 2020 at 13:35
  • I just wondered if perhaps there was an expected, existing alternative implementation that was sufficiently good to negate the bother of adding while await (while await is a purely hypothetical feature BTW). Commented Apr 15, 2020 at 13:37
  • 4
    So... what's the question? Why isn't there a feature in ECMAScript? No one proposed it. Feel free to do so, they have a GitHub repo. Also, see Eric Lippert's rant about why features aren't present in C# (second paragraph). Commented Apr 15, 2020 at 13:56
  • When features are implemented, that happens one by one. Or ideally one by one - it might also be several at once. At any rate, not every feature ever needed is created and added at once, otherwise we wouldn't really get a usable thing. And sometimes after adding one feature it later turns out you need another. So, if something is missing it's not necessarily by deliberate omission. Commented Apr 15, 2020 at 13:58
  • 2
    @HereticMonkey THANK YOU! Do you know how long I've searched for this? Not just now - I just spent another 15 minutes but I wanted to link it several more times in the past. I knew I read it but I couldn't even remember where - was it here on SE, on software engineering, on Eric's blog, on Microsoft's blog, or elsewhere. The problem with Mr Lippert is that he says so many good things everywhere. And since many people mention him by name searching "eric lippert <something>" wields many amazing insights from him just not what I'm looking for right now. Commented Apr 15, 2020 at 14:00

1 Answer 1

2

why was while await (a hypothetical feature) not added when for await...of was?

Because it adds unnecessary complexity to the language implementation, and is rarely ever useful.

Generators ought to produce values, not nothings. Ignoring the value is easy by not using the variable, as you did. The main use case is getting values, and that's what the syntax was designed for.

They actually would be useful even in your example. Your generator doesn't deliver nothing, it doesn't deliver "frames", it does deliver the high resolution timestamps that requestAnimationFrame calls resolve with, and you should be using them in your render function for smooth delta animation:

function createGameLoop(store) {
  async function start() {
    for await (const timestamp of frames) 
      render(store, timestamp)
  }
  return { start }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.