当前位置:网站首页>js中对于返回Promise对象的语句如何try catch
js中对于返回Promise对象的语句如何try catch
2022-07-02 06:22:00 【秦时明月之安康】
文章目录
- 前言
- 一、何为Promise?
- 二、代码演示try catch
- 1. 无 async 修饰符,无 new Promise
- 2. 有 async 修饰符,无 new Promise
- 3. 无 async 修饰符,有 new Promise,Promise里面无 async
- 4. 无 async 修饰符,有 new Promise,Promise里面有 async
- 5. 有 async 修饰符,有 new Promise,Promise里面无 async
- 6. 有 async 修饰符,有 new Promise,Promise里面有 async
- 7. 有 async 修饰符,有 await new Promise,Promise里面无 async
- 8. 有 async 修饰符,有 await new Promise,Promise里面有 async
- 三、图解 try catch
- 四、立即执行函数
- 五、异常不catch,代码不会继续往下执行
- 六、参考文档
前言
平时闲下来必须深究技术,生活才有意义,知其然不知其所以然,是很可怕的,注定技术这条路走不远
一、何为Promise?
要理解这个东西,直接看官方文档,传送门
二、代码演示try catch
1. 无 async 修饰符,无 new Promise
function tryError () {
try {
console.log(a)
} catch (e) {
console.log(e)
}
}

对于上面的同步代码,很好理解,错误能够正常捕获
2. 有 async 修饰符,无 new Promise
async function tryError () {
try {
console.log(a)
} catch (e) {
console.log(e)
}
}
tryError()

函数添加了async 修饰符,错误也能够正常捕获,不同的是执行完函数,同时返回了一个Promise对象,试试看,拿到 Promise 对象之后能不能 catch 异常
async function tryError () {
console.log(a)
}
tryError()

在控制台验证之后,发现没问题,异常被成功 catch 了
3. 无 async 修饰符,有 new Promise,Promise里面无 async
function tryError () {
try {
return new Promise((resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()

发现这种情况下,是无法 catch 异常的,那如果在new Promise之后 catch 呢?试试看,
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()

结果发现,在 .catch 里面成功捕获了异常
4. 无 async 修饰符,有 new Promise,Promise里面有 async
function tryError () {
try {
return new Promise(async (resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()

发现这种情况下,是无法 catch 异常的,那如果在new Promise之后 catch 呢?试试看,
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()

结果发现,在 .catch 里面依然无法捕获异常
5. 有 async 修饰符,有 new Promise,Promise里面无 async
async function tryError () {
try {
return new Promise((resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()

发现这种情况下,是无法 catch 异常的,那如果在new Promise之后 catch 呢?试试看,
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()

结果发现,在 .catch 里面成功捕获了异常
6. 有 async 修饰符,有 new Promise,Promise里面有 async
async function tryError () {
try {
return new Promise(async (resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()

发现这种情况下,是无法 catch 异常的,那如果在new Promise之后 catch 呢?试试看,
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()

结果发现,在 .catch 里面依然无法捕获
7. 有 async 修饰符,有 await new Promise,Promise里面无 async
async function tryError () {
try {
return await new Promise((resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()

这种情况下,发现成功 catch 了异常,那如果在new Promise之后 catch 呢?试试看,
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()

结果发现,在 .catch 里面成功捕获了异常
8. 有 async 修饰符,有 await new Promise,Promise里面有 async
async function tryError () {
try {
return await new Promise(async (resolve, reject) => {
throw new Error('error')
})
} catch (e) {
console.log(e)
}
}
tryError()

这种情况下,发现不能 catch 异常,那如果在await new Promise之后 catch 呢?试试看,
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()

结果发现,在 .catch 里面依然无法捕获异常
三、图解 try catch

可见Promise中的错误捕获正确的用法是:
在 async 函数内部使用 try catch 捕获异步错误
promise 内部使用 .catch 方法来捕获 promise 内部代码错误
四、立即执行函数
function tryError () {
try {
(async () => {
throw new Error('error')
})()
} catch (e) {
console.log(e)
}
}
tryError()

可见立即执行函数只要带上async修饰符,也是无法 catch 异常的,除非在async之后使用 .catch 来捕获,
function tryError () {
try {
(async () => {
throw new Error('error')
})().catch(e => {
console.log('catch:' + e)
})
} catch (e) {
console.log(e)
}
}
tryError()

结果发现,在 .catch 里面成功捕获了异常
五、异常不catch,代码不会继续往下执行
注意: 异常不catch,代码不会继续往下执行
六、参考文档
边栏推荐
- Warp shuffle in CUDA
- Does the assignment of Boolean types such as tag attribute disabled selected checked not take effect?
- 分布式事务 :可靠消息最终一致性方案
- TensorRT的数据格式定义详解
- 代码技巧——Controller参数注解@RequestParam
- Alibaba cloud MFA binding Chrome browser
- 记录一次RDS故障排除--RDS容量徒增
- ctf三计
- web自动化切换窗口时报错“list“ object is not callable
- Automation - when Jenkins pipline executes the nodejs command, it prompts node: command not found
猜你喜欢

Sentinel rules persist to Nacos

TensorRT的数据格式定义详解
![Data science [viii]: SVD (I)](/img/cb/7bf066a656d49666985a865c3a1456.png)
Data science [viii]: SVD (I)

QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)

Redis——大Key問題

Idea announced a new default UI, which is too refreshing (including the application link)

Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *

ctf-web之练习赛

js中正则表达式的使用

华为MindSpore开源实习机试题
随机推荐
pytest(3)parametrize参数化
Learn about various joins in SQL and their differences
CUDA用户对象
Self cultivation of programmers - Reflection on job hunting
Summary of WLAN related knowledge points
TensorRT的数据格式定义详解
最新CUDA环境配置(Win10 + CUDA 11.6 + VS2019)
MySQL的10大经典错误
Latex 编译报错 I found no \bibstyle & \bibdata & \citation command
Sentinel规则持久化到Nacos
automation - Jenkins pipline 执行 nodejs 命令时,提示 node: command not found
利用NVIDIA GPU将Minecraft场景渲染成真实场景
Idea announced a new default UI, which is too refreshing (including the application link)
程序员的自我修养—找工作反思篇
Redis - hot key issues
Sentinel rules persist to Nacos
FE - Eggjs 结合 Typeorm 出现连接不了数据库
FE - weex 开发 之 使用 weex-ui 组件与配置使用
ShardingSphere-JDBC篇
Pytest (1) case collection rules