|
|
|
|
|
by gruseom
4680 days ago
|
|
I have code that calls back synchronously if a value is in memory and asynchronously if it has to make a network call to get it. It's simple, and I'm not persuaded by this post (or the one it recommends) to make it more complicated. In this particular use case it seems enough to know that the callback may occur asynchronously, so you can't assume that anything will still be in scope. |
|
The issue is that once you start doing serious work with asynchrony -- filtering streams, handling disconnects, backing out of parse errors, and dealing with parallelism all at the same time -- then being able to reason clearly about the control flow is paramount to getting anything done.
If sometimes code below the callback registration will be executed first, and sometimes not, depending on unpredictable runtime state, it's game over. Even if you understand the code and it works now, good luck when a dependency update introduces a new corner case and your app's control flow has been radically changed and you're stuck midnight-debugging the mess.
I guess this is one of the things you learn in the trenches, along with avoiding global variables and not optimizing prematurely.