当前位置:网站首页>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
边栏推荐
- kali最新更新指南
- 20201025 Visual Studio2019 QT5.14 信号和槽功能的使用
- Linux MySQL 5.6.51 Community Generic 安装教程
- CUDA中内置的Vector类型和变量
- Thread hierarchy in CUDA
- ctf三计
- Redis - cluster data distribution algorithm & hash slot
- Name six schemes to realize delayed messages at one go
- 20210306转载如何使TextEdit有背景图片
- Fe - wechat applet - Bluetooth ble development research and use
猜你喜欢
随机推荐
Summary of advertisement business bug replay
构建学习tensorflow
Fe - weex uses a simple encapsulated data loading plug-in as the global loading method
由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
Latest CUDA environment configuration (win10 + CUDA 11.6 + vs2019)
Alibaba cloud MFA binding Chrome browser
20210306转载如何使TextEdit有背景图片
Nodejs - Express middleware modification header: typeerror [err_invalid_char]: invalid character in header content
selenium备忘录:selenium\webdriver\remote\remote_connection.py:374: ResourceWarning: unclosed<xxxx>解决办法
自学table au
MySql索引
Functions of tensorrt
Warp shuffle in CUDA
Warp matrix functions in CUDA
(the 100th blog) written at the end of the second year of doctor's degree -20200818
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
ctf-web之练习赛
Android - Kotlin 下使用 Room 遇到 There are multiple good constructors and Room will ... 问题
Redis - big key problem









