I'm using node.js to search through a sqlite table of books and returning only the books that are not checked out. My question is if the second function that I am passing into stmt.each() will wait for isCheckedOut() to finish before executing?
At the moment it seems to be waiting but I'm worried that if isCheckedOut() takes longer to execute (ie: larger database) then the second function will run too early and won't return all the results.
If the second function is running prematurely, how can I make it wait?
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database('database');
function showAvailableBooks(callback) {
var stmt = db.prepare("SELECT * FROM books");
var results = [];
stmt.each(function (err, row) {
isCheckedOut(row.Barcode, function (err, checkedOut) {
if (!checkedOut) {
results.push({Title: row.Title, Author: row.Author, CallNumber: row.CallNumber});
}
});
}, function () {
callback(results);
});
}
showAvailableBooks(function () {
console.log("# of results: " + results.length);
});