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 plains 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
})*/
;
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 plains 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
})*/
;
Share
Improve this question
edited Mar 20, 2018 at 16:33
Doug Stevenson
318k36 gold badges456 silver badges473 bronze badges
asked Mar 20, 2018 at 16:13
user1279144user1279144
432 silver badges5 bronze badges
2 Answers
Reset to default 9Native 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.
A then following a then/catch will always get executed so long as:
- IF the catch is executed, the code within does not throw an error (if it does, the next catch is executed).
db.collection("posts")
.doc(doc.id)
.set(post)
.then(function(docRef) {
//any code, throws error or not.
})
.catch(function(error) {
console.error("Error saving post : ", error);
//this code does not throw an error.
}).then(function(any){
//will always execute.
});