当前位置:网站首页>Promise's understanding and its instance method
Promise's understanding and its instance method
2022-07-27 05:42:00 【weixin_ forty-six million fifty-one thousand two hundred and si】
Is a programming solution
Three states :pending( Have in hand ),fulfilled( Have succeeded )、rejected( Failed )
characteristic : The state is not affected by the outside world , Only results of asynchronous operations , Decide which state it is currently
Once the state changes, it will not change again (pending–>fufilled,pending–>rejected)
usage :
promise It's a constructor , Used to generate promise example
promise Constructor takes a function as an argument , This function has two arguments
resolve: take promise The state of the object never completes and becomes successful
reject: take promise The state of the object never completes to fail
const p=new Promise(function(resolve,reject){
resolve()// Return the result of asynchronous operation , Passed as a parameter
reject()// Return the result of asynchronous operation , Passed as a parameter
})
promise Instance object of
then: When the instance state changes , It's a new promise example , That is to say promise The reason for chain writing
catch: Used to specify the callback function where the error occurred , Let's pass catch replace then The second parameter in
finally: Used to specify whether promise What is the final state of the object , Will perform the operation
const p=new Promise(function(resolve,reject){
setTimeout(() => {
const time=new Date().getTime()
if(time%2==0){
resolve(' Success data ,time'+time)
}else{
reject(' Failed data ,time'+time)
}
},1000);
})
//then
p.then((value)=>{
console.log(value)
},(reason)=>{
console.log(reason)
})
//catch
p.then((value)=>{
console.log(value);
}).catch((reason)=> {
console.log(reason);
})
//finally
p.finally(()=>{
console.log(' result ');// Failure will result in an error , Success returns directly to
})
What is callback hell , And how to use promise Solve callback hell ,then
function getData(){
return new Promise((resolve,reject)=>{
setTimeout(() => {
console.log(1);
resolve(2)
}, 1000);
})
}
getData().then((value)=>{
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(value);
resolve(3)
}, 2000);
})
}).then((value)=>{
return new Promise((resolve, reject) => {
setTimeout(() => {
console.log(value);
}, 3000);
})
})
promise.all
Used to transfer multiple promise example , Pack it as a promise example
promise.all() Parameters can not be arrays , But it must be iterator Interface
pAll The state of , from p1、p2、p3 To decide , Only when these three are successful ,pAll For success
But as long as there is one failure , Then it's failure , At this time, the return value of the first failed instance , Will pass to pAll Callback function for
If as an instance of a parameter , I've defined myself catch Method , So once it rejected, It won't touch pAll Of catch Method
let p1 = new Promise((resolve, reject) => {
resolve(' success 01');
})
let p2 = new Promise((resolve, reject) => {
reject(' Failure 02 The data of ');
}).catch((reason)=>{
console.log(' Failure ');
})
let p3 = new Promise((resolve, reject) => {
resolve(' success 02');
})
let pAll=Promise.all([p1,p2,p3])
console.log(pAll);
pAll.then((value)=>{
console.log(value);
}).catch((reason)=>{
console.log(reason);
})
The results of multiple requests are merged
function getBannerList() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(' Data of the rotation chart ')
}, 1000);
})
}
function getMusicList() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(' Music data ')
}, 2000);
})
}
function getCateList() {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(' Song list classification data ')
}, 3000);
})
}
function initLoad(){
let All=Promise.all([getBannerList(),getMusicList(),getCateList()])
// console.log(All);
All.then((value)=>{
console.log(value);
})
}
initLoad()
边栏推荐
- Qsort - the sorting function in C language (with void*, callback function knowledge points
- JS中是如何使用for..of来遍历对象
- JS中&&(与)和||(或)操作符的返回值
- 攻防世界-mfw
- First knowledge of C language -- common data types
- How to use for..Of to traverse objects in JS
- Arguments class array in JS
- The difference between for... Of and for... In JS
- 维持登录,路由跳转
- User management - paging
猜你喜欢
随机推荐
JS中深拷贝和浅拷贝的区别
First acquaintance with C language - first acquaintance with pointer
Gallerycms download, installation and configuration
Js中如何进行隐式转换
Getaverse,走向Web3的远方桥梁
ES6中new一个箭头函数会怎样
攻防世界-mfw
[极客大挑战 2019]FinalSQL 1
OSI七层模型和TCP/IP四层(TCP与UDP)(笔记)
NFT新范式,OKALEIDO创新NFT聚合交易生态
First knowledge of C language -- what is C language
权限的配置,组件传值
Share a multiple-choice question about the process of program compilation (including a brief discussion on the compilation process, the formation and merging process of symbol tables)
Share a multiple-choice question about define (including the replacement rules, program environment and preprocessing related knowledge of define during precompiling)
Flask blueprint
XSS知识点
Program environment and preprocessing (Part 2): define, undef, command line compilation, conditional compilation, file inclusion (super full collation, recommended collection!!!
C language string function: strlen, strcpy, strcat
每周学习总结
SSTI 模板注入









