3 ms·
Both async generator functions and async iterators have been in the pipeline for a while. The proposed, and currently implemented syntax in Firefox and Chrome i
by daxterspeed 8y ago
Both async generator functions and async iterators have been in the pipeline for a while. The proposed, and currently implemented syntax in Firefox and Chrome is `for await ... of`[0]. Creating an async generator function is as easy as `async function* functionName`.
[0]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for-await...of https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
Example of this syntax in practice: https://jsbin.com/folotu/edit?js,console https://jsbin.com/folotu/edit?js,console
- loige 8y agoI have another article in mind to approach this topic. Also event emitters and streams might be other useful tools of the trade when you have to deal with asynchronous stuff.
- haskman 8y agoYou might be interested in a slightly unusual UI framework I wrote that heavily focuses on async generators to define the UI - https://github.com/ajnsit/concur-js https://github.com/ajnsit/concur-js. The idea is that a UI "Widget" is a sequence of UIs (which themselves can be Widgets) generated asynchronously (say when a UI event fires). A sample widget - async function*() { // Show a clickable button. // All processing is async, this will conceptually block until button is clicked. yield* <button onClick>Click me</button> // Button was clicked, show text yield* <div>Button was clicked!</div> }) It uses React and VDOM to diff the UIs generated and update the page slightly more efficiently. As you can see, it also provides JSX support (by overriding createElement calls). The Widgets themselves can be easily composed, and the entire thing is designed to be extremely easy to get started with. The README on github has more information.