The DatabaseReference.transaction()
method returns a promise which is fulfilled when the transaction is completed. You can use Promise.all()
method to wait for any number of these transaction promises to complete and then call process.exit()
to end the program.
Here is a full example:
var admin = require("firebase-admin");
// Initialize the SDK
var serviceAccount = require("path/to/serviceAccountKey.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});
// Create two transactions
var addRef = admin.database().ref("add");
var addPromise = addRef.transaction(function(current) {
return (current || 0) + 1;
});
var subtractRef = admin.database().ref("subtract");
var subtractPromise = subtractRef.transaction(function(current) {
return (current || 0) - 1;
});
// Wait for all transactions to complete and then exit
return Promise.all([addPromise, subtractPromise])
.then(function() {
console.log("Transaction promises completed! Exiting...");
process.exit(0);
})
.catch(function(error) {
console.log("Transactions failed:", error);
process.exit(1);
});
process.exit()
isn't the best way to shutdown a node process. – Tumblebug