by jaequery · 8y ago · 0 comments
I always wished js was sync by default and only async when you tell it to. Now you have the words async/await (and not too long ago promises, bluebird, waterfall), spread out all over your code for that since you only need async perhaps less than a quarter of your code base. but i dont see js ever becoming procedural first anytime soon.
then I discovered Elixir, i was curious to see how they tout their highly concurrent functional language solved this issue. and surprisingly its as simple as wrapping a block of code inside an async block. its such a pleasant experience finally having best of both worlds, especially coming from years of callback hells and trying to debug what becomes a set of spaghetti code for performing simple things.
I am confused by this. JavaScript is synchronous and blocking by default due to its single-threaded nature as far as I know?
Sure, async will yield different behaviors for functions than without that decoration but that's a very new language feature.
`let res = await someAsyncOperation();`
and whenever you want an async operation (non-blocking), thats when you do something like:
async { let res = someAsyncOperation() }
For example, in Javascript, you would often just have a top-level async function and stay inside async world the whole time.
Putting your example in the middle of some random code can already be done by calling a promise but not putting the downstream code inside its .then() block.
One of the nicest things about Javascript is the absolute simplicity of its concurrency model, one of those perks coming from its single-threaded nature. I look at something like C# and Kotlin with their BYO await semantics as something I'm glad Javascript doesn't have.