Firestore Set Catch最后

use*_*144 1 javascript promise firebase google-cloud-functions google-cloud-firestore

如何在执行Firestore操作后清理资源,我想在保存记录后使用"finally"块关闭对话框但是它抱怨它不是一个函数.我一直在寻找API参考,但我找到的只是入门部分中的几个例子.

我的代码是这样的:

db.collection("posts")
.doc(doc.id)
.set(post)
.then(function(docRef) {
    //todo
})
.catch(function(error) {
    console.error("Error saving post : ", error);
})
/*.finally(function(){
    //close pop up          
})*/
;
Run Code Online (Sandbox Code Playgroud)

Dou*_*son 6

节点6中的本机Promise没有finally()方法.只有then()和catch().(参见此表,节点位于最右侧.)

如果你想在承诺链的末尾无条件地做任何事情而不管成功或失败,你可以在then()和catch()回调中复制它:

doSomeWork()
.then(result => {
    cleanup()
})
.catch(error => {
    cleanup()
})

function cleanup() {}
Run Code Online (Sandbox Code Playgroud)

或者您可以使用TypeScript,它在语言中尝试/ catch/finally定义.