TIL about Concurrency
POSTED ON:
TAGS: programming react
What is Concurrency?
It means tasks can overlap.
(Yoinked this from the comment)
Let's use phone calls as an analogy.
No concurrency means that I can only have one phone conversation at a time. If I talk to Alice, and Bob calls me, I have to finish the call with Alice before I can talk to Bob.
Non-overlapping calls
Concurrency means that I can have more than one conversation at a time. For example, I can put Alice on hold, talk to Bob for a bit, and then switch back to talking to Alice.
Overlapping calls
Note that concurrency doesn't necessarily mean I talk to two people at once. It just means that at any moment, I may be in multiple calls, and I choose who to talk to. For example, based on which conversation is more urgent.
In React's case "phone calls" are your setState calls. Previously, React could only work on one state update at a time. So all updates were "urgent": once you start re-rendering, you can't stop. But with startTransition, you can mark a non-urgent update as a transition.
You can think of "urgent" setState updates as similar to urgent phone calls (e.g. your friend needs your help) while transitions are like relaxed conversations that can be put on hold or even interrupted if they're no longer relevant.
via React-18's ELI5 Discussion thread
Related TILs
Tagged: programming