I get the idea of the readRecords callback, but with a tiny database and a simple app, how does it really work? I need to read the records and then use the data right away, so I need to pause execution for the readRecords to finish, but there isn’t a wait() function or similar. In the code section below, the if (newDay) is executed before readRecords is done, so it isn’t correct. I can do createRecord without a callBack, but not readRecords, because I need access to the magical records object that only exists in the callback, from what I can tell. Is there a way to wait for readRecords to finish before continuing? Project link: Code.org - App Lab
Thanks!
Carol Ramsey
// test to see if records already exist for current day
readRecords(“11G_homework”, {date:today}, function(records) {
console.log ("homework records length = " + records.length);
if (records.length == 0) newDay = true;
else newDay = false;
});
console.log ("newDay = " + newDay);
// if no records for today, create default records
if (newDay) {
// create default 11G records for the day (dones and helps), all set to false
for (var i = 0; i < students.length(); i++) {
for (var j = 0; j < subjects.length(); j++) {
dones[i][j] = false;
helps[i][j] = false;
createRecord(“11G_homework”, {studentID: i, subjectID: j, done: false, help:false}, doNothing);
}
}
function doNothing(){}