Hacker News new | ask | show | jobs
by traderjane 2245 days ago
> A stateful function with effects. Your language just doesn’t have a primitive to express it.

I wonder what about generator functions?

2 comments

Yep, there was even a library making the rounds recently that leverages them: https://crank.js.org/
Another example: Redux Saga has been using them in React for a long time. https://redux-saga.js.org/
I don't see how generators would change anything here. "with effects", "stateful", and the integration between the two are all equally important in the statement.
There are two different concepts as I see it, please correct me if your definitions differ.

"stateful": A function that has state, i.e. can store data

"with effects": A function that modifies data outside its own scope

Normal JS functions (as opposed to arrow function) already do this:

function foo(){};

foo.state = bar;

Generator functions take it a step further, where the function remembers internal state between calls.

Well generators are a good primitive to represent "stateful" "functions" "with effects". The concept of hooks, which somehow give the control back to React to do something, map marvelously to react.

I'd be surprised if generators didn't come into React at one point or another

What are generators for you?