4 ms·
I have avoided yield() and generators like the plague because they're hard to read and I've never seen them used except in examples explaining how they work. Yi
by fenwick67 6y ago
I have avoided yield() and generators like the plague because they're hard to read and I've never seen them used except in examples explaining how they work. Yield feels like recursion - a neat trick for some corner cases but generally unnecessary.
- BenoitEssiambre 6y agoThis is how async/await is implemented. I think 'await' is almost just a synonym for yield. I didn't invent that part. I just made a faster, better encapsulated, promise free version.
- Kwantuum 6y agoI'm failing to see how async/await has more promises than your implementation.
- BenoitEssiambre 6y agoAsync/await requires passing a promise to await. If you read the documentation for async/await, it starts with: "These features basically act as syntactic sugar on top of promises": https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Asynchronous/Async_await https://developer.mozilla.org/en-US/docs/Learn/JavaScript/As...
- galaxyLogic 6y agoJavaScript syntax for generators is clunky no doubt. But they let you do some things more naturally than anything else in JavaScript. They keep track of state "implicitly" you don't have to do an assignment to 'save a state", you just yield and your state is remembered. I found generators useful for implementing my home-brew parsers. Parsers must try out all possible combinations of possible syntactic elements before they can say that parse failed. Generators are a good tool for that
- alcover 6y ago> Parser in JS Insteresting ! Would you mind sharing how you did ?
- galaxyLogic 6y agoThe basic idea is that a generator yields one potential parse of a section of input-string. The caller continues checking the rest of the string but if it fails to find all expected syntactic elements it will go back to the generator basically asking it: Your previous suggestion did not work out, can you give me another possibility?