当前位置:网站首页>Callback function ----------- callback
Callback function ----------- callback
2022-07-06 13:51:00 【qq_ fifty-nine million seventy-six thousand seven hundred and s】
One . What is a callback function
according to MDN Description of : The callback function is a function , It will be executed immediately after the execution of another function , A callback function is passed as a parameter to another JavaScript Function of function . This callback function will be executed inside the function passed to .
Two . Why call back
client JavaScrpit Run... In a browser , And the main process of the browser is a single thread time loop . If we try to perform long-running operations in single threaded events , Will stop the process . Technically speaking, this is not good , Because the process stops processing other events while waiting for completion .
for example .alert Statements are treated as browsers JavaScript One of the blocking codes in . If you run alert, Then close alert Before the dialog window , You won't be able to interact in the browser . To prevent blocking long-running operations , We used callback .
3、 ... and . Example

Cool and finish your meal , Can help people bring things , You can also take it without helping others , But rice must be eaten .
Code :
function liliang(task) {
console.log(` Liangliang eats lunch by himself `);
task()
}
liliang(
function () {
console.log(` Help Ranran buy steamed stuffed buns `);
}
)Pass a function as a parameter , The reason why the parameter is not written dead , You can entrust more people to do .
Four .JavaScript Is the callback asynchronous ?
JavaScript It is considered a single threaded scripting language , Single thread means Javascrpit Execute one code block at a time . When Javascrpit Busy executing a block , It is impossible to move to the next block .
let me put it another way , We can argue that Javascrpit Code is always blocked by nature . But this kind of blocking makes it impossible for us to write code in some cases , Because in these cases, we can't get immediate results when performing certain tasks .
The tasks I'm talking about include the following :
1. adopt API Call to get the data .
2. Get some resources from the remote server by sending a network request .
To deal with these situations , You must write asynchronous code , Callback function is a way to deal with these situations . So essentially , The callback function is asynchronous .
5、 ... and . About the callback to hell
When more than one asynchronous function is executed one by one , Or create callback hell .
adopt async Function to solve the problem of callback hell
async yes ES7 New features , Indicates that the current function is asynchronous , It will not block the thread and cause subsequent code to stop running .
Example :
async function Fn() {
return 'hello world';
}
console.log(Fn());The result returned :

Conclusion :
async Indicates that there is an asynchronous operation in the function
await Indicates that the following expression needs to wait for the result .
6、 ... and . Benefits of callback functions
1. Send a notice at the right time
2. Make the code more flexible
3. Improve the operation efficiency of
Icon :

边栏推荐
- 仿牛客技术博客项目常见问题及解答(三)
- 7. Relationship between array, pointer and array
- Zatan 0516
- 实验八 异常处理
- 撲克牌遊戲程序——人機對抗
- Analysis of penetration test learning and actual combat stage
- [during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
- 受检异常和非受检异常的区别和理解
- [the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
- 强化学习系列(一):基本原理和概念
猜你喜欢

使用Spacedesk实现局域网内任意设备作为电脑拓展屏

MATLAB打开.m文件乱码解决办法

MySQL lock summary (comprehensive and concise + graphic explanation)

Canvas foundation 1 - draw a straight line (easy to understand)

Differences among fianl, finally, and finalize

FAQs and answers to the imitation Niuke technology blog project (I)

It's never too late to start. The tramp transformation programmer has an annual salary of more than 700000 yuan

Have you encountered ABA problems? Let's talk about the following in detail, how to avoid ABA problems

C语言入门指南

. How to upload XMIND files to Jinshan document sharing online editing?
随机推荐
附加简化版示例数据库到SqlServer数据库实例中
撲克牌遊戲程序——人機對抗
[graduation season · advanced technology Er] goodbye, my student days
7-6 矩阵的局部极小值(PTA程序设计)
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
C language to achieve mine sweeping game (full version)
Detailed explanation of redis' distributed lock principle
深度强化文献阅读系列(一):Courier routing and assignment for food delivery service using reinforcement learning
. Net6: develop modern 3D industrial software based on WPF (2)
编写程序,模拟现实生活中的交通信号灯。
8. C language - bit operator and displacement operator
The difference between cookies and sessions
记一次猫舍由外到内的渗透撞库操作提取-flag
Mode 1 two-way serial communication is adopted between machine a and machine B, and the specific requirements are as follows: (1) the K1 key of machine a can control the ledi of machine B to turn on a
力扣152题乘数最大子数组
实验四 数组
[the Nine Yang Manual] 2022 Fudan University Applied Statistics real problem + analysis
C语言入门指南
Using qcommonstyle to draw custom form parts
【九阳神功】2017复旦大学应用统计真题+解析