Promises, async functions, and requestAnimationFrame, together.

/#!/JoePea joe at trusktr.io
Fri Apr 22 23:38:41 UTC 2016


Is it possible?

 I thought maybe something like this:

```js
function animationFrame() {
    let resolve = null
    const promise = new Promise(r => resolve = r)
    window.requestAnimationFrame(resolve)
    return promise
}

async function game() {
    // the game loop
    while (true) {
        await animationFrame()
        drawSomething()
    }
}

game()
```

But, I'm not sure what to expect: does the code that follows the await
statement execute in the frame, sometimes in the frame, or never in
the frame? What might we expect from the various browsers?


More information about the es-discuss mailing list