首页 > 解决方案 > 在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执行块?谢谢你。thencatch

标签: javascript

解决方案


你只需要重新排序链...

Promise.resolve('do somthing')
  .finally(() => console.log('finally called'))
  .then(() => console.log('successfull'))
  .catch(() => console.log('error!'))


推荐阅读