Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.


JavaScript 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


> Parser in JS

Insteresting ! Would you mind sharing how you did ?


The 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?


This 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.


I'm failing to see how async/await has more promises than your implementation.


Async/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/As...




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: