


In Ajax applications, calling XMLHttpRequest is a very common situation. Especially for client-centered Ajax applications, various operations that need to obtain data from the server are completed through XHR asynchronous calls. However, in single-threaded JavaScript programming, the code style of XHR asynchronous calls is really incompatible with general JavaScript code.
Extra parameters
Consider a divide function. If it were a purely client-side synchronous function, the signature would be like this:
function divide(operand1, operand2 )
However, suppose we are not satisfied with the accuracy of the client-side division, so we transfer the division to the server-side for execution. Then it is an asynchronous function that needs to call XHR, and the signature may be one of the following: :
function divide(operand1, operand2, callback)
function divide(operand1, operand2, successCallback, failureCallback)
function divide(operand1, operand2, options)
We have to introduce new parameters in the signature to pass the callback function, There is no option to make the function a blocking synchronous call.
Transportability
Not only do functions that directly operate XHR need to introduce new parameters, this complexity will also be passed along the call stack. For example, we have encapsulated the four arithmetic operations of addition, subtraction, multiplication and division, and only exposed one operation interface:
function calculate(operand1, operand2, operator)
This calculate function is based on the operator parameter. Call the internal plus, subtract, multiply, divide functions. However, because the divide function becomes an asynchronous function, the entire calculate function has to be transformed into an asynchronous function:
function calculate(operand1, operand2, operator, callback)
At the same time, when calling Any function on the stack that needs to be called to calculate must become asynchronous, unless it does not need to return results from the upper level call function.
Synchronous coexistence
Although the calculate function becomes an asynchronous function, the plus, subtract, and multiply functions it calls are still synchronous functions. Only when divide is called, it is asynchronous. At this time, calculate is an asynchronous synchronization function. Concurrent functions.
What problems will this cause? When the caller of calculate sees the function signature, he will naturally think that calculate is an asynchronous function because it needs to pass a callback function. However, the execution method of calculate is uncertain. Consider the following call:
calculate(operand1, operand2, operator, callback);
next();
This involves two functions, callback and next, which one is executed first and which one is executed last The execution is uncertain, or depends on the specific implementation of calculate.
If the implementation of calculate is, when asynchronous operations are not required, callback is called directly. Then, callback will be called before next when performing addition, subtraction, and multiplication; next will be called before callback when performing division.
If we don’t like this uncertainty, we can change the implementation of calculate and change all synchronous calls to setTimeout form, so that next will definitely be called before callback under any circumstances.
However, the latter approach relies on a higher-cost implementation. Developers may accidentally miss setTimeout (or are just plain lazy), causing the function calling sequence to become uncertain, so we would like to This is a function that the framework helps achieve, and there is no way around it when using the framework.
Scenario
Here, I will give a specific application scenario regarding the above problem. (To simplify the problem, the description has been slightly modified, which is not consistent with the actual application.)
In Baidu Hi web version, we will save a list of user objects on the client, and open the chat window with this user When, we need to read this user's information from it. This operation involves many branches that may be synchronous or asynchronous:
The user object is not cached
Read user information asynchronously
The user object is cached
The user is a friend (information updates will be updated by the server client push)
Read user information synchronously
The user is not a friend (information updates need to be pulled from the client)
Cache information can be accepted
Read user information synchronously
The latest information must be obtained
Read user information asynchronously
You can see that the results of the branch are both synchronous and asynchronous. And these branches are not completed in one function, but in several functions. That is to say, according to the traditional model, some of these functions are synchronous and some are asynchronous. Due to the transitivity of asynchronous, the functions at the top of the final call stack are all asynchronous.
In order to solve this problem, we need to write an asynchronous calling framework, use a unified way to make calls, and combine synchronous and asynchronous calls into one return method.

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment