roadrunnertwice: DTWOF's Lois in drag. Dialogue: "Dude, just rub a little Castrol 30 weight into it. Works for me." (Castrol (Lois))
[personal profile] roadrunnertwice

I think I’ve finally wrapped my head around promise-based async logic in JavaScript. It took a while!

To celebrate, here’s the analogy I wish someone had given me ages ago:

A promise is a black hole. Once a value crosses the event horizon into a promise, it can never come back out again to interact with synchronous code. It’s in another universe now! You can send values from synchronous code in there to interact with the promise universe, but they’ll also have to stay there once they cross the event horizon. And it turns out that this is all fine and doesn’t limit you very much at all; it’s just that when you’re in the promise universe, any logic has to be of the form “do this once this condition is met,” never just plain “do this.” Different physics inside the black hole.

Anyway, once I started thinking of it like that, it all seemed perfectly reasonable. It also clarified some things that seemed arbitrary before, like why you can’t call await unless you’re in an async function. (It’s because await is just a clever balancing of terms so you can think synchronously while still obeying async physics.) I gather there ARE other languages that have promises where you can be like “no really, block the main thread and wait for this to resolve,” but since JS’s whole design philosophy treats blocking as such an apocalyptic event, it makes sense that you can never come back from async.