当前位置:网站首页>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 :
边栏推荐
- This time, thoroughly understand the MySQL index
- (原创)制作一个采用 LCD1602 显示的电子钟,在 LCD 上显示当前的时间。显示格式为“时时:分分:秒秒”。设有 4 个功能键k1~k4,功能如下:(1)k1——进入时间修改。
- [modern Chinese history] Chapter 9 test
- MySQL lock summary (comprehensive and concise + graphic explanation)
- 7-3 构造散列表(PTA程序设计)
- Detailed explanation of redis' distributed lock principle
- Intensive literature reading series (I): Courier routing and assignment for food delivery service using reinforcement learning
- Service ability of Hongmeng harmonyos learning notes to realize cross end communication
- FAQs and answers to the imitation Niuke technology blog project (II)
- Miscellaneous talk on May 27
猜你喜欢
2. First knowledge of C language (2)
2. C language matrix multiplication
Principles, advantages and disadvantages of two persistence mechanisms RDB and AOF of redis
附加简化版示例数据库到SqlServer数据库实例中
Mortal immortal cultivation pointer-2
7-7 7003 组合锁(PTA程序设计)
仿牛客技术博客项目常见问题及解答(一)
Read only error handling
MySQL事务及实现原理全面总结,再也不用担心面试
扑克牌游戏程序——人机对抗
随机推荐
4. Binary search
Implementation principle of automatic capacity expansion mechanism of ArrayList
Simply understand the promise of ES6
Redis的两种持久化机制RDB和AOF的原理和优缺点
Detailed explanation of redis' distributed lock principle
简单理解ES6的Promise
Programme de jeu de cartes - confrontation homme - machine
扑克牌游戏程序——人机对抗
Reinforcement learning series (I): basic principles and concepts
附加简化版示例数据库到SqlServer数据库实例中
[modern Chinese history] Chapter 9 test
7. Relationship between array, pointer and array
一段用蜂鸣器编的音乐(成都)
ArrayList的自动扩容机制实现原理
[the Nine Yang Manual] 2021 Fudan University Applied Statistics real problem + analysis
杂谈0516
【黑马早报】上海市监局回应钟薛高烧不化;麦趣尔承认两批次纯牛奶不合格;微信内测一个手机可注册俩号;度小满回应存款变理财产品...
7-4 散列表查找(PTA程序设计)
MATLAB打开.m文件乱码解决办法
The latest tank battle 2022 - Notes on the whole development -2