当前位置:网站首页>Promise基本用法 20211130
Promise基本用法 20211130
2022-07-26 22:39:00 【bug我来写】
promise 是一个拥有 then 方法的对象或函数,其行为符合本规范;then 方法接受两个参数 onFulfilled, onRejected 。前者是成功的回调,后者是失败的回调。如果Promise里面执行的是resolve进入onFulfilled,执行reject进入onRejected。
new Promise((reslove, reject) => {
console.log(1111);
setTimeout(() => {
console.log('setTimeut', 2222);
reslove(1);
}, 1000);
}).then(
res => {
console.log(3333);
},
err => console.log(err),
);
// 1111
// setTimeout 2222
// 3333
Promise.all
使用场景:一次请求多个接口,需要拿到全部信息后进行操作。
let promise1 = Promise.resolve(1111);
let promise2 = new Promise((resolve, reject) => {
setTimeout(resolve, 1000, 2222); // setTimeout的第三个参数会传入setTimeout第一个参数也就是执行方法里
});
let promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 3333);
});
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
}).catch((err) => console.log(err));
// [1111, 2222, 3333]
只要任意一个Promise变成rejected状态都会走catch
let promise1 = Promise.resolve(1111);
let promise2 = new Promise((resolve, reject) => {
setTimeout(reject, 1000, 2222);
});
let promise3 = new Promise((resolve, reject) => {
setTimeout(resolve, 2000, 3333);
});
Promise.all([promise1, promise2, promise3]).then((values) => {
console.log(values);
}).catch((err) => console.log(err));
// 2222
// promise2 里面换成了reject,直接走catch
Promise.allSettled
无论是有失败的请求,他都会走then并用对象将结果包起来
let promise1 = Promise.resolve(1111);
let promise2 = new Promise( (resolve, reject) => {
setTimeout(reject, 1000, 2222);
});
let promise3 = new Promise( (resolve, reject) => {
setTimeout(resolve, 2000, 3333);
});
Promise.allSettled([promise1, promise2, promise3])
.then((values) => {
console.log(values);
})
.catch((err) => console.log(err));
// [
// { status: 'fufilled', value: 1111 },
// { status: 'rejected', value: 2222 },
// { status: 'fufilled', value: 3333 }
// ]
Promise.race
多个请求中,那个先执行就返回该请求的结果
let promise2 = new Promise( (resolve, reject) => {
setTimeout(resolve, 1000, 2222);
});
let promise3 = new Promise( (resolve, reject) => {
setTimeout(resolve, 2000, 3333);
});
Promise.race([ promise2, promise3])
.then( (values) => {
console.log(values);
})
// 2222
Promise 值穿透 及如何中断Promise 链式请求
穿透
Promise.resolve(1)
.then(2) // 注意这里
.then(Promise.resolve(3))
.then(console.log); // 输出1 这里写法等于 .then(e => console.log(e))
return 一个值修改下一个then里面的value
Promise.resolve(1)
.then(() => 2)
.then(Promise.resolve(3))
.then(console.log) // 输出2
Promise.resolve(1)
.then(() => 2)
.then(() => Promise.resolve(3))
.then(console.log) // 输出3
中断Promise只有一种方法,那就是让他的状态变始终停留在pending
Promise是有三种状态 pending,fulfilled,rejected
const promise = new Promise((resolve, reject) => {
resolve(111);
}).then((res) => {
console.log(res) //打印111
return 222
}).then(res => {
console.log(res) //打印222
return new Promise(() => {
})
}).then(res => {
console.log(3333) //不会打印,被中断了
})
边栏推荐
- 【Codeforces Round #808 (Div 2.) A·B·C】
- 【 Educational Codeforces Round 132 (Rated for Div. 2) A·B·C】
- DOM day_01(7.7) dom的介绍和核心操作
- 【AcWing第61场周赛】
- 关于Redis问题的二三事
- 13_ Ensemble learning and random forests
- 【4.4 快速幂详解及快速幂求逆元】
- [4.10 detailed explanation of game theory]
- [4.1 prime number and linear sieve]
- 2022_ SummerBlog_ 008
猜你喜欢

3_Jupyter Notebook, numpy和matplotlib

Signal and system learning zero input response

Crop TIF image

2020-12-20 99 multiplication table

Nacos installation and pit stepping
![[4.9 detailed explanation of inclusion exclusion principle]](/img/c9/673507abab48a1593486c2901adac9.png)
[4.9 detailed explanation of inclusion exclusion principle]

DOM day_ 02 (7.8) web page production process, picture SRC attribute, carousel chart, custom attribute, tab bar, input box event, check operation, accessor syntax

The use of C language static can flexibly change the life cycle and make you write code like a duck to water

ArcGIS and CASS realize elevation points of cross-section Exhibition

Two or three things about redis
随机推荐
C语言 求素数、闰年以及最小公倍数最大公约数
Shang school software testing (1) software testing curriculum system, advantages, learning suggestions, understanding software, software testing and defects, software testing process, debugging and te
DOM day_ 03 (7.11) event bubbling mechanism, event delegation, to-do items, block default events, mouse coordinates, page scrolling events, create DOM elements, DOM encapsulation operations
简单的素数程序 初学者 希望码龄高的大佬可以一下
【 Educational Codeforces Round 132 (Rated for Div. 2) A·B·C】
用New,delete和用malloc,free申请,释放堆区空间
CDs simulation of minimum dominating set based on MATLAB
[LeetCode] 无重复最长字符串
[PCB open source sharing] stc8a8k64d4 development board
输入一串字母 将里面的元音输出希望各位大佬能给指导
DOM day_ 02 (7.8) web page production process, picture SRC attribute, carousel chart, custom attribute, tab bar, input box event, check operation, accessor syntax
Web middleware log analysis script 2.0 (shell script)
2020-12-22最大公因数
13_ Ensemble learning and random forests
JSCORE day_05(7.6)
Blue Bridge Cup 1004 [recursive] cow story
Eight queens n Queens
【4.1 质数及线性筛】
【Codeforces Round #807 (Div 2.) A·B·C】
[interview: concurrent Article 16: multithreading: detailed explanation of wait/notify] principle and wrong usage (false wake-up, etc.)