当前位置:网站首页>How to try catch statements that return promise objects in JS
How to try catch statements that return promise objects in JS
2022-07-02 06:40:00 【The health of the bright moon in Qin Dynasty】
List of articles
- Preface
- One 、 What is the Promise?
- Two 、 Code demonstration try catch
- 1. nothing async Modifier , nothing new Promise
- 2. Yes async Modifier , nothing new Promise
- 3. nothing async Modifier , Yes new Promise,Promise No inside async
- 4. nothing async Modifier , Yes new Promise,Promise There are async
- 5. Yes async Modifier , Yes new Promise,Promise No inside async
- 6. Yes async Modifier , Yes new Promise,Promise There are async
- 7. Yes async Modifier , Yes await new Promise,Promise No inside async
- 8. Yes async Modifier , Yes await new Promise,Promise There are async
- 3、 ... and 、 The illustration try catch
- Four 、 Immediate execution function
- 5、 ... and 、 Abnormal no catch, The code will not continue to execute
- 6、 ... and 、 Reference documents
Preface
You must study technology deeply when you are idle , Life is meaningful , Know what it is and don't know why it is , It's terrible , It is doomed that technology will not go far
One 、 What is the Promise?
To understand this thing , Look directly at the official documents , Portal
Two 、 Code demonstration try catch
1. nothing async Modifier , nothing new Promise
function tryError () {
try {
console.log(a)
} catch (e) {
console.log(e)
}
}
For the above synchronization code , Well understood. , Errors can be captured normally
2. Yes async Modifier , nothing new Promise
async function tryError () {
try {
console.log(a)
} catch (e) {
console.log(e)
}
}
tryError()
Function added async Modifier , Errors can also be captured normally , The difference is that after executing the function , At the same time, a Promise object , Give it a try , Get Promise After the object can catch abnormal
async function tryError () {
console.log(a)
}
tryError()
After console verification , No problem , Abnormally successful catch 了
3. nothing async Modifier , Yes new Promise,Promise No inside async
function tryError () {
try {
return new Promise((resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()
Find out in this case , It's impossible catch Anomalous , Well, if it's in new Promise after catch Well ? Give it a try ,
function tryError () {
try {
return new Promise((resolve, reject) => {
throw new Error('error')
}).then(res => {
console.log(res)
}).catch(e => {
console.log('catch:' + e)
})
} catch (e) {
console.log(e)
}
}
tryError()
Results found , stay .catch The exception was successfully caught
4. nothing async Modifier , Yes new Promise,Promise There are async
function tryError () {
try {
return new Promise(async (resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()
Find out in this case , It's impossible catch Anomalous , Well, if it's in new Promise after catch Well ? Give it a try ,
function tryError () {
try {
return new Promise(async (resolve, reject) => {
throw new Error('error')
}).then(res => {
console.log(res)
}).catch(e => {
console.log('catch:' + e)
})
} catch (e) {
console.log(e)
}
}
tryError()
Results found , stay .catch It still cannot catch exceptions
5. Yes async Modifier , Yes new Promise,Promise No inside async
async function tryError () {
try {
return new Promise((resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()
Find out in this case , It's impossible catch Anomalous , Well, if it's in new Promise after catch Well ? Give it a try ,
async function tryError () {
try {
return new Promise((resolve, reject) => {
throw new Error('error')
}).then(res => {
console.log(res)
}).catch(e => {
console.log('catch:' + e)
})
} catch (e) {
console.log(e)
}
}
tryError()
Results found , stay .catch The exception was successfully caught
6. Yes async Modifier , Yes new Promise,Promise There are async
async function tryError () {
try {
return new Promise(async (resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()
Find out in this case , It's impossible catch Anomalous , Well, if it's in new Promise after catch Well ? Give it a try ,
async function tryError () {
try {
return new Promise(async (resolve, reject) => {
throw new Error('error')
}).then(res => {
console.log(res)
}).catch(e => {
console.log('catch:' + e)
})
} catch (e) {
console.log(e)
}
}
tryError()
Results found , stay .catch It is still unable to capture
7. Yes async Modifier , Yes await new Promise,Promise No inside async
async function tryError () {
try {
return await new Promise((resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()
In this case , Find success catch It's abnormal , Well, if it's in new Promise after catch Well ? Give it a try ,
async function tryError () {
try {
return await new Promise((resolve, reject) => {
throw new Error('error')
}).then(res => {
console.log(res)
}).catch(e => {
console.log('catch:' + e)
})
} catch (e) {
console.log(e)
}
}
tryError()
Results found , stay .catch The exception was successfully caught
8. Yes async Modifier , Yes await new Promise,Promise There are async
async function tryError () {
try {
return await new Promise(async (resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()
In this case , Find that you can't catch abnormal , Well, if it's in await new Promise after catch Well ? Give it a try ,
async function tryError () {
try {
return await new Promise(async (resolve, reject) => {
throw new Error('error')
}).then(res => {
console.log(res)
}).catch(e => {
console.log('catch:' + e)
})
} catch (e) {
console.log(e)
}
}
tryError()
Results found , stay .catch It still cannot catch exceptions
3、 ... and 、 The illustration try catch
so Promise The correct usage of error capture in is :
stay async Function internal use try catch Catch asynchronous errors
promise For internal use .catch Method to capture promise Internal code error
Four 、 Immediate execution function
function tryError () {
try {
(async () => {
throw new Error('error')
})()
} catch (e) {
console.log(e)
}
}
tryError()
It can be seen that the immediate execution function is as long as you bring async Modifier , It's impossible catch Anomalous , Except in async Then use .catch To capture ,
function tryError () {
try {
(async () => {
throw new Error('error')
})().catch(e => {
console.log('catch:' + e)
})
} catch (e) {
console.log(e)
}
}
tryError()
Results found , stay .catch The exception was successfully caught
5、 ... and 、 Abnormal no catch, The code will not continue to execute
Be careful : Abnormal no catch, The code will not continue to execute
6、 ... and 、 Reference documents
JS Medium async/await The usage and understanding of
Use Promise
promise Error capture in
边栏推荐
- Functions of tensorrt
- Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
- FE - 微信小程序 - 蓝牙 BLE 开发调研与使用
- Kotlin - 验证时间格式是否是 yyyy-MM-dd HH:mm:ss
- automation - Jenkins pipline 执行 nodejs 命令时,提示 node: command not found
- unittest. Texttestrunner does not generate TXT test reports
- Codeforces Round #797 (Div. 3) A—E
- FE - Weex 使用简单封装数据加载插件为全局加载方法
- qq邮箱接收不到jenkins构建后使用email extension 发送的邮件(timestamp 或 auth.......)
- Fe - weex uses a simple encapsulated data loading plug-in as the global loading method
猜你喜欢
Distributed transactions: the final consistency scheme of reliable messages
CUDA中的Warp Shuffle
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
Warp shuffle in CUDA
Fe - wechat applet - Bluetooth ble development research and use
VSCODE 安装LATEX环境,参数配置,常见问题解决
TensorRT的数据格式定义详解
Data science [9]: SVD (2)
Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *
Latex 报错 LaTeX Error: The font size command \normalsize is not defined问题解决
随机推荐
CUDA中的Warp Shuffle
Linux MySQL 5.6.51 Community Generic 安装教程
Hydration failed because the initial UI does not match what was rendered on the server. One of the reasons for the problem
Fe - eggjs combined with typeorm cannot connect to the database
Detailed definition of tensorrt data format
selenium+msedgedriver+edge浏览器安装驱动的坑
unittest.TextTestRunner不生成txt测试报告
JS modification element attribute flipping commonly used in selenium's Web Automation
20210306 reprint how to make TextEdit have background pictures
Sentinel规则持久化到Nacos
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
Sentinel rules persist to Nacos
Latex在VSCODE中编译中文,使用中文路径问题解决
Solution to the black screen of win computer screenshot
QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)
Pytest (3) parameterize
kali最新更新指南
Data science [9]: SVD (2)
20201002 vs 2019 qt5.14 developed program packaging
重载全局和成员new/delete