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

**URL:** <https://forum.code.org/t/how-can-i-pause-for-the-readrecords-callback-to-finish-before-i-try-to-use-the-data/35296>\
**Category:** Website and Tools Questions\
**Tags:** csd-unit-2\
**Created:** [October 19, 2021, 9:42pm UTC](https://forum.code.org/t/how-can-i-pause-for-the-readrecords-callback-to-finish-before-i-try-to-use-the-data/35296 "2021-10-19T21:42:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![carol.ramsey2](https://avatars.discourse-cdn.com/v4/letter/c/67e7ee/32.png) [@carol.ramsey2](https://forum.code.org/u/carol.ramsey2)\
**Post date:** [October 19, 2021, 9:42pm UTC](https://forum.code.org/t/how-can-i-pause-for-the-readrecords-callback-to-finish-before-i-try-to-use-the-data/35296/1 "2021-10-19T21:42:02Z")

</div>

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](https://studio.code.org/projects/applab/NTmuCC1huDAEISs4uoTVeYCuGuF7r6LUtMGhB7giHsg)  
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(){}

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [October 21, 2021, 3:31pm UTC](https://forum.code.org/t/how-can-i-pause-for-the-readrecords-callback-to-finish-before-i-try-to-use-the-data/35296/2 "2021-10-21T15:31:33Z")

</div>

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

Look at this [Assigning value from getKeyValue to a variable](https://forum.code.org/t/assigning-value-from-getkeyvalue-to-a-variable/35251)

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.

---

<div class="post-metadata">

**Author:** ![jdonwells](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.code.org/jdonwells/32/7900_2.png) [@jdonwells](https://forum.code.org/u/jdonwells)\
**Post date:** [October 21, 2021, 5:41pm UTC](https://forum.code.org/t/how-can-i-pause-for-the-readrecords-callback-to-finish-before-i-try-to-use-the-data/35296/3 "2021-10-21T17:41:28Z")

</div>

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:

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

```

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