当前位置:网站首页>Promise对象与宏任务、微任务
Promise对象与宏任务、微任务
2022-07-25 15:06:00 【深海蓝山】
Promise 是异步编程的一种解决方案,可以让异步操作写起来,就像在写同步操作的流程,而不必一层层地嵌套回调函数。
promise有三个状态:
1、pending[待定]初始状态
2、fulfilled[实现]操作成功
3、rejected[被否决]操作失败
一旦状态改变,就不会再改变,当promise状态发生改变,就会触发then()里的响应函数处理后续步骤
Promise.all()
Promise的all方法提供了并行执行异步操作的能力,并且在所有异步操作执行完后才执行回调.用Promise.all来执行,all接收一个数组参数,里面的值最终都算返回Promise对象。这样,三个异步操作的并行执行的,等到它们都执行完后才会进到then里面
Promise.race()
race方法与all正好相反,谁最先执行完成,就以谁为准执行回调。
function p1() {
console.log('p1开始');
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('p1结束');
resolve('p1的返回结果');
}, 1000);
});
return p;
}
function p2() {
console.log('p2开始');
var p = new Promise(function (resolve, reject) {
setTimeout(function () {
console.log('p2结束');
resolve('p2的返回结果');
}, 1000);
});
return p;
}
Promise.all([p1(), p2()])
.then((result) => {
console.log('全部返回完成');
console.log(result);
})//p1开始 p2开始 p1结束 p2结束 全部返回完成
Promise.race([p1(), p2()])
.then((result) => {
console.log('全部返回完成');
console.log(result);
})//p1开始 p2开始 p1结束 全部返回完成Promise的执行顺序
Promise是宏任务(同步执行),但Promise 的回调函数属于异步任务,会在同步任务之后执行(比如说 then、 catch 、finally)。
Promise 的回调函数不是正常的异步任务,而是微任务(microtask)。它们的区别在于,正常任务追加到下一轮事件循环,微任务追加到本轮事件循环。这意味着,微任务的执行时间一定早于正常任务。
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);
})
//执行顺序:1 3 4 6 5 2
执行顺序 1 3 4 6 5 2,这说明then的回调函数的执行时间,早于setTimeout(fn, 0)。因为then是微任务是本轮事件循环执行,setTimeout(fn, 0)在下一轮事件循环开始时执行。
边栏推荐
- Login of MySQL [database system]
- 流程控制(上)
- Leetcode-198- house raiding
- [C topic] force buckle 876. Intermediate node of linked list
- 35 quick format code
- [C topic] Li Kou 206. reverse the linked list
- 37 元素模式(行内元素,块元素,行内块元素)
- "Ask every day" briefly talk about JMM / talk about your understanding of JMM
- Browser based split screen reading
- QT connect, signal, slot and lambda comparison
猜你喜欢

SPI传输出现数据与时钟不匹配延后问题分析与解决

Raft of distributed consistency protocol

基于AMD EPYC服务器的EDA芯片设计解决方案
[Android] recyclerview caching mechanism, is it really difficult to understand? What level of cache is it?

oracle_12505错误解决方法

SQL优化的一些建议,希望可以帮到和我一样被SQL折磨的你

VS2010 add WAP mobile form template
![[Nacos] what does nacosclient do during service registration](/img/76/3c2e8f9ba19e36d9581f34fda65923.png)
[Nacos] what does nacosclient do during service registration

The solution to the problem that the progress bar of ros2 installation connext RMW is stuck at 13%

阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
随机推荐
简易轮播图和打地鼠
Overview of cloud security technology development
Thymeleaf notes
C, c/s upgrade update
Login of MySQL [database system]
I hope some suggestions on SQL optimization can help you who are tortured by SQL like me
Scala111-map、flatten、flatMap
VS2010 add WAP mobile form template
easygui使用的语法总结
"Ask every day" what is volatile
BigDecimal rounds the data
27 选择器的分类
pkg_resources动态加载插件
37 元素模式(行内元素,块元素,行内块元素)
Detailed explanation of lio-sam operation process and code
[MySQL series] - how much do you know about the index
万能通用智能JS表单验证
Ssh server rejected password
Solve the error caused by too large file when uploading file by asp.net
Live classroom system 05 background management system