当前位置:网站首页>Preliminary understanding of promse
Preliminary understanding of promse
2022-04-23 16:59:00 【Endless cake】
Promse What is it? ?
- Abstract expression :Promise yes JS New solution for asynchronous programming in ( I used to use callback Callback function )
- Specific expression :
(1) Grammatically :Promise It's a constructor
(2) functionally :Promise Object is used to encapsulate an asynchronous operation and get its results
Why use Promise?
One .
- Specifying callback functions is more flexible
- You must specify... Before starting an asynchronous task
- Promise: Start asynchronous task => return Promise object => to Promise Bind callback function , Even after the asynchronous task is finished , Result of callback request
Two .
1.Promise Support chain calls , It can solve the problem of callback to hell
What is callback hell ?
Multiple asynchronous requests are called in series , And callback , There will be a callback to hell
When the callback function is nested and called , Not easy to read , It is also not convenient for exception handling
Promise The code of chain call to solve callback hell is as follows :
// Calls that are not nested relationships , So there will be no problem of callback to hell
fn1().then((result)=>{
return fn1(result)
})
.then((result2)=>{
return fn2(result2)
})
.then((result3)=>{
console.log('success')
}).catch(()=>{// Abnormal transmission . Any problem will be transmitted to catch
console.log('error')
})
The perfect solution to callback hell is actually async/await
Pure synchronous coding
async function request(){
try{
const result = await fn1()
const result2 = await fn2()
const result3 = await fn3 ()
console.log(' final result :'+result3)
}catch(error){
console.log('error')
}
}
版权声明
本文为[Endless cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554519763.html
边栏推荐
- TypeError: set_ figure_ params() got an unexpected keyword argument ‘figsize‘
- [pimf] openharmony paper Club - what is the experience of wandering in ACM survey
- vscode如何比较两个文件的异同
- Sub database and sub table & shardingsphere
- About stream flow, write it down briefly------
- VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN
- [problem solving] [show2012] random tree
- PostgreSQL column storage and row storage
- Variable length parameter__ VA_ ARGS__ Macro definitions for and logging
- Pycham connects to the remote server and realizes remote debugging
猜你喜欢

Loggie source code analysis source file module backbone analysis

Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption

如何建立 TikTok用户信任并拉动粉丝增长

feign报400处理

NVIDIA graphics card driver error

Derivation of Σ GL perspective projection matrix

博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生

Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears

How much do you know about the process of the interview

Do you really understand the principle of code scanning login?
随机推荐
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
_ Mold_ Board_
Idea of batch manufacturing test data, with source code
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
VsCode-Go
Error in v-on handler: "typeerror: cannot read property 'resetfields' of undefined"
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
正则过滤内网地址和网段
织梦DEDECMS安全设置指南
BUG_ me
Knowledge points and examples of [seven input / output systems]
Pseudo Distributed installation spark
Do you really understand the principle of code scanning login?
oracle 中快速获取表的列名列表
ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型
Get the column name list of the table quickly in Oracle
文件操作《二》(5000字总结篇)
1-3 nodejs installation list configuration and project environment
Modify the test case name generated by DDT
MySQL master-slave configuration under CentOS