当前位置:网站首页>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
边栏推荐
- Mock test
- 1-1 NodeJS
- Paging the list collection
- TypeError: set_figure_params() got an unexpected keyword argument ‘figsize‘
- Log4j output log information to file
- How vscode compares the similarities and differences between two files
- Milvus 2.0 質量保障系統詳解
- Nodejs reads the local JSON file through require. Unexpected token / in JSON at position appears
- Get the column name list of the table quickly in Oracle
- File upload and download of robot framework
猜你喜欢
The new MySQL table has a self increasing ID of 20 bits. The reason is
On lambda powertools typescript
Detailed explanation of Niuke - Gloves
Knowledge points and examples of [seven input / output systems]
NVIDIA graphics card driver error
VLAN advanced technology, VLAN aggregation, super VLAN, sub VLAN
Zhongang Mining: Fluorite Flotation Process
RTKLIB 2.4.3源码笔记
Lock锁
如何建立 TikTok用户信任并拉动粉丝增长
随机推荐
Summary of common websites
Redis docker installation
CentOS MySQL multi instance deployment
Handwritten event publish subscribe framework
SPC introduction
PHP高效读大文件处理数据
SQL database
◰GL-着色器处理程序封装
Use case labeling mechanism of robot framework
org. apache. parquet. schema. InvalidSchemaException: A group type can not be empty. Parquet does not su
Pycham connects to the remote server and realizes remote debugging
MySQL modify master database
1-1 NodeJS
MySQL master-slave replication
详解牛客----手套
Dlib of face recognition framework
【题解】[SHOI2012] 随机树
feign报400处理
[registration] tf54: engineer growth map and excellent R & D organization building
Read a blog, re understand closures and tidy up