Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firestore Set Catch Finally

How do I clean up resources after doing a Firestore operation, I want to use the "finally" block to close a dialog after saving the record but it complains it is not a function. I been searching for the API reference but all I find is the few examples in the getting started section.

my code is something like this:

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          
})*/
;
like image 734
user1279144 Avatar asked Dec 12 '25 08:12

user1279144


1 Answers

Native Promises in node 6 don't have a finally() method. There is just then() and catch(). (See this table, node is on the far right.)

If you want do do something unconditionally at the end of a promise chain regardless of success or failure, you can duplicate that in both then() and catch() callbacks:

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

function cleanup() {}

Or you can use TypeScript, which has try/catch/finally defined in the language.

like image 120
Doug Stevenson Avatar answered Dec 13 '25 22:12

Doug Stevenson