当前位置:网站首页>Promise object and macro task, micro task
Promise object and macro task, micro task
2022-07-25 15:12:00 【Deep sea blue mountains】
Promise Is a solution to asynchronous programming , You can write asynchronous operations , It's like writing the flow of synchronous operation , Instead of nesting callback functions layer by layer .
promise There are three states :
1、pending[ undetermined ] The initial state
2、fulfilled[ Realization ] Successful operation
3、rejected[ Rejected ] operation failed
Once the status changes , It won't change , When promise The state changes , It will trigger then() The response function in handles the next steps
Promise.all()
Promise Of all Methods provide the ability to perform asynchronous operations in parallel , And the callback is executed after all asynchronous operations have been executed . use Promise.all To execute ,all Receive an array parameter , All the values in it are finally returned Promise object . such , Parallel execution of three asynchronous operations , Wait until they're all done then Inside
Promise.race()
race Methods and all Just the opposite , Who is the first to complete , The callback will be executed according to who .
function p1() {
console.log('p1 Start ');
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('p1 end ');
resolve('p1 Return result of ');
}, 1000);
});
return p;
}
function p2() {
console.log('p2 Start ');
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('p2 end ');
resolve('p2 Return result of ');
}, 1000);
});
return p;
}
Promise.all([p1(), p2()])
.then((result) => {
console.log(' All return completed ');
console.log(result);
})//p1 Start p2 Start p1 end p2 end All return completed
Promise.race([p1(), p2()])
.then((result) => {
console.log(' All return completed ');
console.log(result);
})//p1 Start p2 Start p1 end All return completed Promise Execution order of
Promise It's a macro task ( Synchronous execution ), but Promise The callback function of is an asynchronous task , It will be executed after the synchronization task ( for instance then、 catch 、finally).
Promise The callback function of is not a normal asynchronous task , It's a micro task (microtask). The difference is , The normal task is appended to the next round of event loop , The micro task is added to this round of event loop . It means , The execution time of micro task must be earlier than that of normal task .
console.log(1);
setTimeout(() => {
console.log(2);
}, 10);
new Promise(function(resolve,reject){
console.log(3);
resolve('');
console.log(4);
}).then(res=>{
console.log(5);
})
console.log(6);
})
// Execution order :1 3 4 6 5 2
Execution order 1 3 4 6 5 2, This explanation then The execution time of the callback function of , Before setTimeout(fn, 0). because then It is a micro task that is executed in this round of events ,setTimeout(fn, 0) Execute at the beginning of the next event cycle .
边栏推荐
猜你喜欢
随机推荐
Introduction to raspberry Pie: initial settings of raspberry pie
TypeScript学习1——数据类型
图片裁剪cropper 示例
什么是物联网
pkg_resources动态加载插件
LeetCode_因式分解_简单_263.丑数
"How to use" decorator mode
记一次Spark foreachPartition导致OOM
在win10系统下使用命令查看WiFi连接密码
期货在线开户是否安全?去哪家公司手续费最低?
[C topic] the penultimate node in the Niuke linked list
Single or multiple human posture estimation using openpose
When using jetty to run items, an error is reported: form too large or form too many keys
Meanshift clustering-01 principle analysis
一个程序最多可以使用多少内存?
Copy files / folders through Robocopy
Process control (Part 1)
[C topic] Li Kou 206. reverse the linked list
outline和box-shadow实现外轮廓圆角高光效果
C, c/s upgrade update








![[Nacos] what does nacosclient do during service registration](/img/76/3c2e8f9ba19e36d9581f34fda65923.png)
