当前位置:网站首页>Methods to realize asynchrony
Methods to realize asynchrony
2022-06-26 00:23:00 【Senora】
The way to achieve asynchrony
One 、 Callback function
A callback is a function that is passed as an argument to another function , Do not execute until that function is finished .( That is to say :B The function is passed as an argument to A In the function , stay A After the function is executed, execute B )
Callback function is the most basic method of asynchronous programming , Callbacks are not necessarily asynchronous
principle : Use the working principle of the timer to f1 Put it into the event queue to execute , Even if the delay is 0, So it is with , Therefore, the program is not blocked . Here you can master the working principle of the timer .
Suppose there are two functions f1 and f2, The latter waits for the execution result of the former .
advantage : Simple , Easy to understand and deploy .
shortcoming : Not conducive to the reading and maintenance of the code , The parts are highly coupled , The process is chaotic , And each task can only specify one callback function .
f1();
f2();
// If f1 It's a time-consuming task , Consider rewriting f1, hold f2 It's written in f1 Callback function for .
function f1(callback){
setTimeout(function () {
// f1 The task code of
callback();
}, 1000);
}
// Executing the code will look like this :
f1(f2);
Two 、 Event monitoring
Another idea is to use event driven mode . Task execution does not depend on the order of the code , It depends on whether an event happens .
principle : Make use of the principle of timer to f1 Put it in the event queue , When it's all done , Will execute the methods in the event queue .
Or to f1 and f2 For example . First , by f1 Bind an event ( Here we use jQuery Writing ).
advantage : Easier to understand , Multiple events can be bound , Each event can specify multiple callback functions , And it can “ Decouple ”, It's good for modularity .
shortcoming : The whole program should be event driven , The running process will become very unclear .
f1.on('done', f2);
// The above line of code means , When f1 happen done event , Is executed f2. then , Yes f1 To rewrite :
function f1(){
setTimeout(function () {
// f1 The task code of
f1.trigger('done');
}, 1000);
}
f1.trigger(‘done’) Express , After execution , Immediately triggered done event , So we can start to execute f2.
3、 ... and 、Promise object
Promise The object is CommonJS A specification proposed by the working group , The purpose is to provide a unified interface idea for asynchronous programming , Each asynchronous task returns one Promise object , The object has one then Method , Allows you to specify callback functions .
such as ,f1 Callback function for f2, It can be written. :f1().then(f2);
advantage : Callbacks become chained , The process of the program can be seen clearly , And there's a whole set of matching methods , Many powerful functions can be realized .
shortcoming : Write and understand , It's relatively difficult .
function f1(){
var dfd = $.Deferred();
setTimeout(function () {
// f1 The task code of
dfd.resolve();
}, 500);
return dfd.promise;
}
边栏推荐
- Circuit board edge removal - precautions for V-CUT splitting machine
- Farsync simple test
- Is camkiia the same as gcamp6f?
- JS to input the start time and end time, output the number of seasons, and print the corresponding month and year
- 19c安装psu 19.12
- Ffmpeg version switching
- 10.4.1 données intermédiaires
- idea设置mapper映射文件的模板
- anchor free dection简介
- 2021-04-28
猜你喜欢

【ROS进阶篇】第一讲 常用API介绍
![[advanced ROS] Lecture 1 Introduction to common APIs](/img/25/85e8c55605f5cc999a8e85f0a05f93.jpg)
[advanced ROS] Lecture 1 Introduction to common APIs

How ASA configures port mapping and pat

Thrift getting started

Installation of third-party library iGraph for social network visualization

leetcode.14 --- 最长公共前缀

Camkiia et gcamp6f sont - ils les mêmes?

小红书微服务框架及治理等云原生业务架构演进案例

Redis memory elimination mechanism

什么是微服务
随机推荐
The problem of low video memory in yolov5 accelerated multi GPU training
linux安装redis
CaMKIIa和GCaMP6f是一样的嘛?
Installation of third-party library iGraph for social network visualization
Redux workflow + complete code of small examples
Research and development practice of Kwai real-time data warehouse support system
Deploy Ogg on the same machine and test
安装PSU的时候/usr/bin/ld:warning: -z lazyload ignore
darkent2ncnn出错
2021-04-28
元宇宙中的法律与自我监管
SMT Mounter workflow
Use js to obtain the last quarter based on the current quarter
CaMKIIa和GCaMP6f是一樣的嘛?
10.4.1 data console
正则表达式介绍及一些语法
EBS R12.2.0升级到R12.2.6
EBS r12.2.0 to r12.2.6
Setting up a cluster environment under Linux (2) -- installing MySQL under Linux
no_expand和use_concat