当前位置:网站首页>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
边栏推荐
- Paging the list collection
- 正则过滤内网地址和网段
- PostgreSQL column storage and row storage
- Get the column name list of the table quickly in Oracle
- SPC introduction
- ∑GL-透视投影矩阵的推导
- Customize my_ Strcpy and library strcpy [analog implementation of string related functions]
- freeCodeCamp----prob_ Calculator exercise
- _ Mold_ Board_
- Project framework of robot framework
猜你喜欢

1-1 NodeJS

VLAN高级技术,VLAN聚合,超级Super VLAN ,Sub VLAN

Nacos + aspnetcore + Ocelot actual combat code

PyMySQL

Quick install mongodb

ACL 2022 | DialogVED:用于对话回复生成的预训练隐变量编码-解码模型

Milvus 2.0 détails du système d'assurance de la qualité

SQL database

Selenium IDE and XPath installation of chrome plug-in

Project framework of robot framework
随机推荐
VsCode-Go
Pseudo Distributed installation spark
Feign report 400 processing
Installation and management procedures
PHP efficiently reads large files and processes data
Handwritten event publish subscribe framework
Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
DDT + Excel for interface test
Bytevcharts visual chart library, I have everything you want
【解决报错】Error in v-on handler: “TypeError: Cannot read property ‘resetFields’ of undefined”
True math problems in 1959 college entrance examination
正则过滤内网地址和网段
Regular filtering of Intranet addresses and segments
Tencent resolves the address according to the IP address
Shell脚本——Shell编程规范及变量
1-3 nodejs installation list configuration and project environment
STM32__03—初识定时器
关于局域网如何组建介绍
Generate random numbers with high quality and Gaussian distribution
Use case labeling mechanism of robot framework