当前位置:网站首页>Promise 二:关键问题
Promise 二:关键问题
2022-08-03 09:29:00 【要不要买菜啊】
1. 如何改变 promise 的状态?
2. 一个 promise 指定多个成功/失败回调函数, 都会调用吗?
let p = new Promise((resolve, reject) => {
// resolve('OK');
});
///指定回调 - 1
p.then(value => {
console.log(value);
});
//指定回调 - 2
p.then(value => {
alert(value);
});3. 改变 promise 状态和指定回调函数谁先谁后?
(2) 如何先改状态再指定回调?
let p = new Promise((resolve, reject) => {
resolve('OK');
});
p.then(value => {
console.log(value);
},reason=>{
})
// 先执行回调,再改变状态
let p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('OK');
}, 1000);
});
p.then(value => {
console.log(value);
},reason=>{
})4. promise.then()返回的新 promise 的结果状态由什么决定?
5. promise 如何串连多个操作任务?
let p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('OK');
}, 1000);
});
p.then(value => {
return new Promise((resolve, reject) => {
resolve("success");
});
}).then(value => {
console.log(value);
}).then(value => {
console.log(value);
})
// promise success
// undefined
//.then的确返回promise对象,但这个对象的值由回调返回的值决定,这里没有声明返回值,所以返回undefined,那么下一个then获取到的值就是undefined,直接打印出来
6. promise 异常传透?
let p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('OK');
// reject('Err');
}, 1000);
});
p.then(value => {
// console.log(111);
throw '失败啦!';
}).then(value => {
console.log(222);
}).then(value => {
console.log(333);
}).catch(reason => {
console.warn(reason);
});
// 失败啦7. 中断 promise 链?
let p = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('OK');
}, 1000);
});
p.then(value => {
console.log(111);
//有且只有一个方式
return new Promise(() => {});
}).then(value => {
console.log(222);
}).then(value => {
console.log(333);
}).catch(reason => {
console.warn(reason);
});
// 111边栏推荐
猜你喜欢
随机推荐
pytorch one-hot 小技巧
10 minutes to get you started chrome (Google) browser plug-in development
SAP Analytics Cloud 和 SAP Cloud for Customer 两款 SaaS 软件的集成
Validate floating point input
redis实现分布式锁的原理
【LeetCode】zj面试-把字符串转换成整数
Industry SaaS Microservice Stability Guarantee Actual Combat
命令行加载特效 【cli-spinner.js】 实用教程
面试突击71:GET 和 POST 有什么区别?
10分钟带你入门chrome(谷歌)浏览器插件开发
Rabbit and Falcon are all covered, Go lang1.18 introductory and refined tutorial, from Bai Ding to Hongru, the whole platform (Sublime 4) Go lang development environment to build EP00
箭头函数与普通函数的区别
WinCheck Script
手把手教你如何自制目标检测框架(从理论到实现)
固件工程师到底是干什么?
MySQL-存储过程-函数-
【LeetCode】226.翻转二叉树
阿里云·短信发送
播放量暴涨2000w+,单日狂揽24w粉,内卷的搞笑赛道还有机会
MySQL_关于JSON数据的查询









