当前位置:网站首页>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 :
边栏推荐
- FAQs and answers to the imitation Niuke technology blog project (III)
- canvas基础1 - 画直线(通俗易懂)
- 实验四 数组
- 关于双亲委派机制和类加载的过程
- [modern Chinese history] Chapter 6 test
- [the Nine Yang Manual] 2017 Fudan University Applied Statistics real problem + analysis
- Canvas foundation 2 - arc - draw arc
- Reinforcement learning series (I): basic principles and concepts
- 力扣152题乘数最大子数组
- [au cours de l'entrevue] - Comment expliquer le mécanisme de transmission fiable de TCP
猜你喜欢
[面试时]——我如何讲清楚TCP实现可靠传输的机制
1. C language matrix addition and subtraction method
MySQL lock summary (comprehensive and concise + graphic explanation)
【VMware异常问题】问题分析&解决办法
Poker game program - man machine confrontation
3. Input and output functions (printf, scanf, getchar and putchar)
canvas基础2 - arc - 画弧线
[during the interview] - how can I explain the mechanism of TCP to achieve reliable transmission
PriorityQueue (large root heap / small root heap /topk problem)
Service ability of Hongmeng harmonyos learning notes to realize cross end communication
随机推荐
5. Function recursion exercise
Leetcode.3 无重复字符的最长子串——超过100%的解法
【毕业季·进击的技术er】再见了,我的学生时代
实验七 常用类的使用
Read only error handling
MySQL锁总结(全面简洁 + 图文详解)
Redis的两种持久化机制RDB和AOF的原理和优缺点
[the Nine Yang Manual] 2018 Fudan University Applied Statistics real problem + analysis
A piece of music composed by buzzer (Chengdu)
String abc = new String(“abc“),到底创建了几个对象
6. Function recursion
Analysis of penetration test learning and actual combat stage
7-7 7003 组合锁(PTA程序设计)
Mortal immortal cultivation pointer-1
Brief introduction to XHR - basic use of XHR
简单理解ES6的Promise
1143_ SiCp learning notes_ Tree recursion
This time, thoroughly understand the MySQL index
Zatan 0516
4. Branch statements and loop statements