- Mastering JavaScript Promises
- Muzzamil Hussain
- 827字
- 2021-07-16 13:46:47
Learning the JavaScript asynchronous model
Keeping this knowledge in mind, if we see what the JavaScript asynchronous model is, we can now clearly relate to an asynchronous model in JavaScript and understand how it's implemented.
In non-web languages, most of the code we write is synchronous, that is, blocking. JavaScript does its stuff in a different way.
JavaScript is a single-threaded language. We already know what single threaded actually means for the sake of simplicity—two bits of the same script cannot run at the same time. In browsers, JavaScript shares a thread with loads of other processes inline. These "inline processes" can be different from one browser to another, but typically, JavaScript (JS) is in the same queue as painting, updating styles, and handling user actions (an activity in one of these processes delays the others).
As in the image beneath, whenever the asynchronous (non-blocking) script executes in a browser, it goes from top to bottom in an execution pattern. Starting from the page load, the script goes to a document object where the JavaScript object is created. The script then goes into the parsing phase where all the nodes and HTML tags are added. After the completion of parsing, the whole script will be loaded in the memory as an asynchronous (non-blocking) script.
How JavaScript implements an asynchronous model
JavaScript uses an loop event and its cycle is called a "tick" (as in a clock), since it runs within the time slot bound by the CPU. An interpreter is responsible for checking whether every tick is an asynchronous callback to be executed. All other synchronous operations take place within the same tick. The time value passed is not guaranteed—there's no way of knowing how long it will take until the next tick, so we usually say the callbacks will run "as soon as possible"; although, some calls may even be dropped.
Within JavaScript, there are four core ways on how an asynchronous model is implemented in it. These four methods help not only for better performance of your program, but also in easier maintainability of code. These four methods are as follows:
- A callback function
- The event listener
- The publisher/subscriber
- The promises object
Callbacks in JavaScript
In JavaScript, functions are first class citizens, which means they can be treated as objects and because of the fact that they really are objects themselves. They can do what a regular object is capable of, such as these:
- Stored in variables
- Passed as augments to other functions
- Created within functions
- Returned from functions after a payload of some processed data mechanism
A callback function, also known as a higher-order function, is a function that is passed to another function (let's call this other function as otherFunction
) as a parameter, and the callback function is called (executed) inside otherFunction
.
A callback function is essentially a pattern (an established solution to a common problem), and therefore the use of a callback function is also known as a callback pattern. Because functions are first class objects, we can use callback functions in JavaScript.
Since functions are first class objects, we can use callback functions in JavaScript, but what are callback functions? The idea behind callback functions is derived from functional programming, which uses functions as arguments as implementing callback functions is as easy as passing regular variables as arguments to functions.
A common use of a callback function can be seen in the following lines of code:
$("#btn_1).click().click.function() { alert ("Button one was clicked"); });
The code explains itself as follows:
- We pass a function as a parameter to the
click
function - The
click
function will call (or execute) the callback function we passed to it
This is a typical use of callback functions in JavaScript, and indeed, it is widely used in jQuery. We will examine promise with respect to jQuery in more details in Chapter 8, Promises in jQuery.
Blocking functions
While we are discussing what a blocking function in JavaScript is and how one should implement it, many of us really don't clearly understand what we mean by a blocking function in JavaScript.
As humans, we have a mind that is designed in such a way that it can do many tasks at a time, such as while reading this book, you are aware of the surroundings around you, you can think and type simultaneously, and you can talk to someone while you are driving.
These examples are for multithreaded models, but is there any blocking function in our human body? The answer is yes. We have a blocking function because of which we all have other activities in our mind and within our body; it stops for a tiny pinch of a nanosecond. This blocking function is called sneezing. When any human sneezes, all the functions related to mind and body became blocked for a tiny fraction of nanosecond. This is rarely noticed by people. The same goes with the blocking function of JavaScript.
- Cocos2d Cross-Platform Game Development Cookbook(Second Edition)
- 移動UI設計(微課版)
- Python測試開發入門與實踐
- Python零基礎快樂學習之旅(K12實戰訓練)
- 差分進化算法及其高維多目標優化應用
- D3.js 4.x Data Visualization(Third Edition)
- 軟件品質之完美管理:實戰經典
- 微服務從小白到專家:Spring Cloud和Kubernetes實戰
- 基于ARM Cortex-M4F內核的MSP432 MCU開發實踐
- 大話Java:程序設計從入門到精通
- Azure Serverless Computing Cookbook
- Visual FoxPro 6.0程序設計
- Kotlin Programming By Example
- LabVIEW數據采集
- Spring Data JPA從入門到精通