Image for post

Image for post

— Keep learning, keep building and keep growing :)

Javascript is a single-threaded language. This means it has one call stack and one memory heap. As expected, it executes code in order and must finish executing a piece code before moving onto the next. It’s synchronous, but at times that can be harmful. For example, if a function takes a while to execute or has to wait on something, it freezes everything up in the meanwhile.

A good example of this happening is the window alert function.

alert("Hello World")

You can’t interact with the webpage at all until you hit OK and dismiss the alert. You’re stuck.

So how do we get asynchronous code with Javascript then?

Well, we can thank the Javascript engine (V8, Spidermonkey, JavaScriptCore, etc…) for that, which has Web API that handles these tasks in the background. The call stack recognizes functions of the Web API and hands them off to be handled by the browser. Once those tasks are finished by the browser, they return and are pushed onto the stack as a callback.

Open your console and type window then press enter. You’ll see most everything the Web API has to offer. This includes things like ajax calls, event listeners, the fetch API, and setTimeout. Javascript uses low-level programming languages like C++ to perform these behind the scenes.

#javascript #asynchronous-programming #call-stack #event-loop #multithreading #programming

JavaScript Asynchronous — Theory
1.45 GEEK