3 ms·
What's the reasoning for using this over promise and/or async?
by whatwherewhy 4y ago
What's the reasoning for using this over promise and/or async?
- vanderZwan 4y agoFair question! Well, look at the part in the code example in the second link where it goes: // then either cancel, drag/drop, or click yield* coro.first( /* ... */ ); In this example that function takes three generator functions. Whichever of the three yields a value first "goes through", and coro.first() then aborts the other two. The resulting code reads a lot like how you would describe it: "first detect a click on the rectangle, then either cancel the repositioning if escape key is pressed, move the rectangle if the mouse moves, or drop it if a click happens" The structure is a lot more like the "if/else" kind of control flow structures that most people are more familiar with. On top of that it's deterministic (technically, single-threaded use of things like setTimeout also are but because of how you would structure this it is easier to reason about). Another way to look at it would be to say that this way of expressing things aligns better with solving the problem in terms of state machines (and with UI that often is quit a nice approach). This is know as the structured synchronous concurrency paradigm and it's actually quite nice for certain types of (singlethreaded) concurrency, especially complex UI events. Céu is a language that goes a bit deeper into this, as well as the Blech language (both targeting embedded contexts - button presses changing machine settings are places where FSM are a natural fit). http://ceu-lang.org/ http://ceu-lang.org/ https://www.blech-lang.org/ https://www.blech-lang.org/
- jbluepolarbear 4y agoI use generator coroutines pretty extensively in my game framework. It makes for some clean cooperative code that’s fun to write. https://github.com/jbluepolarbear/Bumble https://github.com/jbluepolarbear/Bumble
- vanderZwan 4y agoHey that looks nice, but looks also like it had no activity for five years (I guess it was stable enough?) Do you have a link to games written with it too? :)
- jbluepolarbear 4y agoYeah, it’s fairly stable for what I use it for. I mainly use it for game jams and have done a few professional projects with it. Here’s an asteroids game I made with the framework work: https://github.com/jbluepolarbear/Bumble-Asteroids https://github.com/jbluepolarbear/Bumble-Asteroids Here’s a playable link: https://www.jeremyiscool.com/Bumble-Asteroids/index.html https://www.jeremyiscool.com/Bumble-Asteroids/index.html
- vanderZwan 4y agoNice!
- whatwherewhy 4y agoHey, this is very cool. I might try making some minigames. Thanks for sparking that interest in me again.