当前位置:网站首页>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
边栏推荐
- 蓝桥杯省一之路06——第十二届省赛真题第二场
- Interface document yaml
- 1-2 characteristics of nodejs
- How to build tiktok user trust and drive fan growth
- Project framework of robot framework
- How to implement distributed locks with redis?
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
- Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
- Collect blog posts
猜你喜欢
vscode如何比较两个文件的异同
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
Bytevcharts visual chart library, I have everything you want
Construction of promtail + Loki + grafana log monitoring system
Detailed explanation of information abstract, digital signature, digital certificate, symmetric encryption and asymmetric encryption
Milvus 2.0 détails du système d'assurance de la qualité
[PROJECT] small hat takeout (8)
Feign report 400 processing
True math problems in 1959 college entrance examination
feign报400处理
随机推荐
PyTorch:train模式与eval模式的那些坑
Nodejs installation and environment configuration
Multithreaded @ async thread pool
Aiot industrial technology panoramic structure - Digital Architecture Design (8)
[PROJECT] small hat takeout (8)
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
New keyword learning and summary
MySQL master-slave configuration under CentOS
拷贝构造函数 浅拷贝与深拷贝
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
VsCode-Go
freeCodeCamp----shape_ Calculator exercise
freeCodeCamp----prob_ Calculator exercise
CentOS MySQL multi instance deployment
Production environment——
Loggie source code analysis source file module backbone analysis
聊一聊浏览器缓存控制
PostgreSQL column storage and row storage
Encapsulating the logging module
Lock lock