Generators seem like a perfect fit for stateful, asynchronous code – so why are React components built as plain functions?
So basically, generators are a neat way to implement Fibonacci generators in interviews
https://twitter.com/albertgao/status/1256797659207065600
but let me backtrack and ask an entirely different question: what are async functions? they’re functions, which output a promise to the value
generators are another type of function with a special output — they output iterators.
iterator interface
they have a special syntax, and a special yield
operator which outputs the next value of the iterator, and can also return something passed in via next()
in fact, you can have async generators too, which outputs an async iterator. I won’t cover the details on generators though; for that, use trusty mdn. the main thing to keep in mind, is that generators are functions which can output a sequence of values using yield
. and this makes them perfect for defining components.
when you think about it, components are also kinda like generators — they output a sequence of elements.
They can store state in closures.
They can output elements via yield.
And unlike components, they can even await!
here’s what it looks like in crank.js
okay, so generators are a perfect fit for components. obviously, that means React has made a grave mistake by avoiding them. right?
https://twitter.com/james_k_nelson/status/1256792759534379009
Tokyo, Japan