当前位置:网站首页>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) //不会打印,被中断了
})
边栏推荐
- Helicopter control system based on Simulink
- 运算符重载
- Programmers must do 50 questions
- 【 Educational Codeforces Round 132 (Rated for Div. 2) A·B·C】
- 【4.10 博弈论详解】
- Friend友元函数以及单例模式
- Torch. correlation function
- View where Anaconda created the environment
- Oracle data guard service, process and protection mode
- V-viewer use
猜你喜欢

Shufflenet series (2): explanation of shufflenet V2 theory

10_ Evaluate classification

Course notes of Professor Dalin of robotics platform

Anaconda = > pycharm=> CUDA=> cudnn=> pytorch environment configuration

Complete review of parsing web pages

Configure deeplobcut 1 with your head covered

Based on the theoretical principle and simulation results of MATLAB spherical decoding, compare 2norm spherical decoding, infinite norm spherical decoding, ML detection

c语言 static运用,灵活改变生命周期,让你写代码如鱼得水

Comparative simulation of LEACH protocol performance, including the number of dead nodes, data transmission, network energy consumption, the number of cluster heads and load balance

Signal and system impulse response and step response
随机推荐
[leetcode] no duplicate longest string
Matlab simulation of image reconstruction using filtered back projection method
Input a string of letters and output the vowels inside. I hope you guys can give guidance
裁剪tif影像
2022-07-17:1, 2, 3... N-1, N, n+1, n+2... In this sequence, only one number has repetition (n). This sequence is unordered. Find the repeated number n. This sequence is ordered. Find the repeated numb
【4.6 中国剩余定理详解】
蓝桥杯 1004 [递归]母牛的故事
[4.6 detailed explanation of Chinese remainder theorem]
Web middleware log analysis script 2.0 (shell script)
Collection of 3D LUT related articles
[4.7 Gauss elimination details]
Viterbi Viterbi decoding bit error rate simulation, modulation is QPSK, channel is Gaussian white noise
The company gave how to use the IP address (detailed version)
Comparative simulation of LEACH protocol performance, including the number of dead nodes, data transmission, network energy consumption, the number of cluster heads and load balance
The detailed process of reinstalling AutoCAD after uninstallation and deleting the registry
10_ Evaluate classification
【Codeforces Round #808 (Div 2.) A·B·C】
DOM day_02(7.8)网页制作流程、图片src属性、轮播图、自定义属性、标签栏、输入框事件、勾选操作、访问器语法
Class and object notes I
[4.1 prime number and linear sieve]