I would like to convert the following callback procedure to a promise. I have the following:
app.get('/api/books', function(req, res)
{
let booksCallback = function(books)
{
res.send(books)
}
DataBase.getBooks(booksCallback)
})
And then:
getBooks : function(booksCallback)
{
database.ref('/books/').once('value', function(datasnapshot)
{
var books = datasnapshot.val()
booksCallback(books)
});
}
So I'm sending the callback function as a parameter to the function getBooks, which does an async call to Firebase. But is there any way I could do something like Database.getBooks().then(res.send(books))
? But then I would need the variable books returned from the async function in getBooks first. Can this be done with a Promise?