4 ms·
How is this able to pause work, given that the work is already executing on the main thread (and I assume you don't need to rewrite your code to explicity give
by foolswisdom 3y ago
How is this able to pause work, given that the work is already executing on the main thread (and I assume you don't need to rewrite your code to explicity give up control at various points)?
- bastawhiz 3y agoYou await the `yieldOrContinue` function. `yieldOrContinue` returns a promise that resolves when your code should resume running. Your work will only be paused at places that you explicitly await the provided functions, so yes, you do need to explicitly give up control.
- astoilkov 3y agoYes, that's it. You choose where to pause/resume work and place the calls to `yieldOrContinue` there.
- cryptonector 3y agoThe only gotcha is that you have to choose wisely. It's not a bad thing though. This is really cooperative multitasking all over again.
- esprehn 3y agoPretty much, which is how browsers work in general with the single thread (ex. The html parser yields after a certain number of tokens or if user input is waiting). React's concurrent mode is the same thing as well.
- deleted 3y ago[deleted]