为什么react不使用generator实现异步可中断的fiber架构
2023-04-04 14:10:15
为什么react不使用generator来实现异步可中断,而是去研发了fiber架构来实现异步可中断呢
在这个issue中我们可以看到 sebmarkbage 的回复
There's two reasons for this.
Generators doesn't just let you yield in the middle of a stack. You have to wrap every single function in a generator. This not only adds a lot of syntactic overhead but also runtime overhead in any existing implementation. It's fair that the syntax might be more helpful than not, but the perf issue still stands.
The biggest reason, however, is that generators are stateful. You can't resume in the middle of it.
主要原因有两个,第一是因为使用generator会带来一些性能问题,第二也是主要问题是因为generator是有状态的,不能在中断后恢复,并且不能被高优先级的任务打断低优先级的更新