Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
joas19422yI used generator to calculate possibly infinite series of recurring event occurences. It seemed like a good solution.
-
I've used them when I have sequences I want to iterate over and the sequence can be mathematically defined by a parameter(s).
But in the end, it's just syntactic sugar (tho it may have different implications in other js engines) for a loop. -
- Tokenization
- splitting up procressing steps of arrays into many small actions that can be done one at a time
With hack pipes this will be a lot more readable as well.. -
@CoreFusionX It literally pauses the execution of the function with every yield and preserves the state, so more than syntax sugar ..
-
function * previousIds ( lastestId ){
while ( true )
yield lastestId--
}
async function * previousComments ( latestId ){
for ( const id of previousIds(latestId) )
yield await
fetch(`stuff.com/comments?id=${ latestId }`)
.then((response) => response.json())
}
async function * take ( generator , limit ){
for await ( const item of generator ){
if(limit < 1)
return
yield item
limit--;
}
}
function toComment ( json ){
const element = document.createElement('div');
element.innerText = json.content;
return element
}
for await ( const comment of previousComments(lastedId) )
html_comments.appendChild(toHtml(comment));
Could be much nicer with hack pipes..
https://proposals.es/proposals/...
Thanks devRant for removing my identation, who needed it anyways .. -
@Lensflare Brought this up before and SOME people didn't see the point in it..
Anyone here use generator functions in JS? Seems like it could be useful but whenever I explore the possibility of using one, I usually end up going another route
question
javascript
generator