0

Single-Threaded, non-blocking, asynchronously programming Whats is it ?

5th Dec 2020, 6:01 AM
EMMANUEL NUOTAH TUONUO DERY
EMMANUEL NUOTAH TUONUO DERY - avatar
5 Answers
+ 7
EMMANUEL NUOTAH TUONUO DERY Javascript code is executed in a single thread but Javascript runtime is not run in single thread. Even Thread pool exists in JS runtime but don’t worry about it as Runtime takes care of it... Heap stores all the variables and call stack performs the operations! Like example - console.log("Start") function sayHello(name) { console.log(`Hello ${name}!`) } sayHello("Abhinav"); console.log("End"); Might you came up with a question that How is it non-blocking? So like.. Say, when you make a call to an API and it fails or some other event is stuck, it’s still in the Web apis so it never goes to callback queue and hence call stack. So nothing is blocked. if you want to know more about it.. you can go here - https://dev.to/steelvoltage/if-javascript-is-single-threaded-how-is-it-asynchronous-56gd#:~:text=Javascript%20is%20a%20single%20threaded,before%20moving%20onto%20the%20next.
5th Dec 2020, 6:21 AM
Piyush
Piyush - avatar
+ 7
Ore Thanks for clarification! I means to say that when one use api and it fails orcoof some event is stuck.. yet it's present is web APIs! Though don't be confusing because it was just a guess that may the questioner have this question too that "how can It be ""non-blocking""
5th Dec 2020, 9:09 AM
Piyush
Piyush - avatar
5th Dec 2020, 8:36 AM
Ore
Ore - avatar
+ 1
Piyush[21 Dec❤️] I think you are confusing the Web APIs with HTTP APIs. The Web APIs are the set of APIs that browsers provide for working with the browser or user system resources in ways that would otherwise not have been possible. Examples are Canvas API, LocalStorage API, HTMLMediaElement API, CacheStorage API and so on.
5th Dec 2020, 9:14 AM
Ore
Ore - avatar
0
Piyush[21 Dec❤️] Only objects are stored on the heap. Also what do you mean by "...when a call to an API ...fails ...,it's still in the Web apis so it never goes to callback queue"? According to my understanding, when you run some code that has a delay e.g accessing an external API, this is what happens 1. The runtime initiates the action and moves to the next stack frame. 2. When the event of success or failure occurs, a new message is added to the queue. 3. Once the event loop reaches its turn, the message is processed. If no event occurs e.g an unused promise, the result is completely discarded.
5th Dec 2020, 8:54 AM
Ore
Ore - avatar