Asynchronous Programming

Azareal

Paragon
Joined
Dec 18, 2010
Messages
1,680
Reaction score
353
FP$
4,498
Asynchronous programming, in contrast to traditional synchronous programming is where you have multiple things going on in parallel.

You might be doing some computation before running a query, and then, while it waits for the result of that query, it'll switch and do another task in the meanwhile, so that time isn't wasted. Asides from having compilers which spit out optimised assembly (one JIT and the other AOT), it's one of the main reasons that JavaScript and Go are so fast.

With the traditional model, a thread would sit around and do nothing until the OS decides that the thread has been running for long enough and does an expensive context switch to another thread or it finally receives a response from the database.
With the asynchronous model, the time the database takes to respond matters a lot less and you can achieve performance unheard of compared to the traditional model.

With Go, there are light-weight "threads" called goroutines which run largely synchronous code within them, each request is run as it's own goroutine by a HTTP server which is also written in Go and you can spawn your own. And when you hit a bit of blocking code like a query, Go will swap out that goroutine for another which has been paused and will execute that instead.

JavaScript has an asynchronous model largely inspired by C#s built around callbacks. You register callbacks for events, and when those events fire, for instance a query responding, it'll run those as soon as possible.

Unlike Go and C# however, JavaScript runs everything on a single thread meaning that only one thing can ever run at once, so to take full advantage of the cores on your machine, you have to run multiple processes, although they're taking steps to alleviate this somewhat.

JS and C# however, have a lot more boilerplate and require you to understand asynchronous programming in general, unlike Go where you call a function and the system automatically switches to another goroutine for you without you having to use any special keywords, register callbacks, etc.

Go and C# however, are strongly typed languages, so you have to explicitly specify types for functions and variables, unlike JS, although it could be argued that this makes functions self-documenting as you know what types they expect (something modern programmers do anyway in comments) and reduces the numbers of bugs as a script can tell you upfront if you've done something stupid without having to see if the program works or not.

Strong typing can also alleviate or eliminate certain classes of security vulnerabilities.
It also opens the door to a large number of optimisations.

Fun Facts:
JavaScript is handled by the major browsers aka Mozilla, Microsoft, Apple, Google.
Go was created by and developed by Google and the Community.
C# was well, obviously, created by Microsoft who seem to no longer hate Linux and have apparently embraced it.
 
Last edited:
Go and C# however, are strongly typed languages, so you have to explicitly specify types for functions and variables, unlike JS
If you use Typescript, you can do that in JS too. 😀
 
Back
Top Bottom