当前位置:网站首页>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)在下一轮事件循环开始时执行。
边栏推荐
- I hope some suggestions on SQL optimization can help you who are tortured by SQL like me
- Number of high-quality number pairs [bit operation characteristics + abstract ability evaluation + grouping fast statistics]
- Leo-sam: tightly coupled laser inertial odometer with smoothing and mapping
- LeetCode第 303 场周赛
- 43 盒子模型
- pkg_resources动态加载插件
- [C topic] force buckle 876. Intermediate node of linked list
- When using jetty to run items, an error is reported: form too large or form too many keys
- How many ways can you assign initial values to a two-dimensional array?
- 35 quick format code
猜你喜欢

Overview of cloud security technology development

"Ask every day" briefly talk about JMM / talk about your understanding of JMM

Bridge NF call ip6tables is an unknown key exception handling

Implement a simple restful API server

41 picture background synthesis - colorful navigation map

流程控制(上)

I hope some suggestions on SQL optimization can help you who are tortured by SQL like me

ice 100G 网卡分片报文 hash 问题

VS2010 add WAP mobile form template

Ssh server rejected password
随机推荐
Overview of cloud security technology development
pl/sql 创建并执行oralce存储过程,并返回结果集
万能通用智能JS表单验证
oracle_12505错误解决方法
Solve the error caused by too large file when uploading file by asp.net
[C topic] Li Kou 88. merge two ordered arrays
流程控制(上)
Browser based split screen reading
Boosting之GBDT源码分析
Spark002---spark任务提交,传入json作为参数
About RDBMS and non RDBMS [database system]
VS2010 add WAP mobile form template
Add the jar package under lib directory to the project in idea
[C topic] force buckle 876. Intermediate node of linked list
35 快速格式化代码
How to realize a correct double check locking
MeanShift聚类-01原理分析
C#,C/S升级更新
(original) customize a scrolling recyclerview
Splice a field of the list set into a single string