javascript - 在javascript中的then/catch之前执行一个函数(例如finally)
问题描述
我正在开发electron
. 我的项目有以下代码片段:
showLoading('confirm')
api.deliveries.create(NewDelivery.createDelivery()).then((insertId) => {
NewDelivery.back()
showAlert('alert-success', 'Delivery Saved!', 'Delivery ID: ' + insertId)
}).catch((error) => {
eid('message').innerText = 'Delivery not saved. Try again later or contact the developer'
throw error
}).finally(() => {
finishLoading('confirm')
$('#confirmModal').modal('hide')
})
当块成功执行时,NewDelivery.back
会调用它,它会从 中删除 id 为“confirm”和“confirmModal”的元素,从而DOM
破坏我的应用程序。我的问题是,有没有办法在调用或调用之前finally
执行块?谢谢你。then
catch
解决方案
你只需要重新排序链...
Promise.resolve('do somthing')
.finally(() => console.log('finally called'))
.then(() => console.log('successfull'))
.catch(() => console.log('error!'))
推荐阅读
- node.js - mongoose 5.7.0 中的自定义验证
- puppet - 同一行重复声明错误
- ionic-framework - IONIC 4 ERROR ionic cordova build android --prod --release
- java - 访问片段内的活动 UI 组件
- rest - 如何为我的 API 实现基于令牌的身份验证?
- c# - 使用 Task 实现 FireAndForget
- html - 向@media 标签添加多个类,为 CSS 清理高效编码?
- sql - Oracle SQL中需要对哪些字符进行转义以避免SQL注入?
- java - JPA查询以检查特定年份月份的记录是否存在?
- c++ - 根据多个信号运行函数