Vishant dhandha Vishant dhandha - 16 days ago 7
Node.js Question

How to convert promise into callback in node js?

Here is simple scenario..

I want to convert these code given in firebase documentation to my api..

How can I convert it to callback function?

var uid = "some-uid";

admin.auth().createCustomToken(uid)
.then(function(customToken) {
// Send token back to client
})
.catch(function(error) {
console.log("Error creating custom token:", error);
});


Here is the link of documentation..

https://firebase.google.com/docs/auth/admin/create-custom-tokens

Answer

If you want to use node-style callbacks on a promise, invoke them like this:

.then(function(result) {
    callback(null, result);
}, function(error) {
    callback(error);
});

Some promise libraries also have helper functions for that, like Bluebirds .asCallback(callback).

Comments