How can I pause for the readRecords callback to finish before I try to use the data?

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: - App Lab

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(){}

You are struggling with event driven programming. You don’t wait; you call.

Look at this Assigning value from getKeyValue to a variable

Very little in your program should reside outside of a function. It all chains from events that are set into motion by clicking on something.

The first thing I am seeing is that you are disassembling the records you get back from the database. I would not do that. I would work with them in JSON format.

So the first part looks like this:

var students;
// #1 read STUDENTS (static)
var currentStudent;
readRecords("11G_students", {}, function(records) {
  if (records.length > 0) {
    students = records;
    setProperty("studentDropDown", "options",{return}));
  } else {
    console.log ("no student records");

You want to change the dropdown to a list of names after you get them.