For the reading a table script shown below, I am trying to run the query in the parameter and for each result trying to append a value from a different table to the result. But due to asynchronous nature of azure, request.respond() is always called before getInvites. Which means that the results are never appended with invites.
function read(query, user, request) {
request.execute({
success: function (results) {
for (var i = 0; i < results.length; i++) {
getInvites(results[i].id, function (invites) {
console.log("Assigning results.invites"); //runs second
results[i].invites = invites;
});
}
console.log("Request Responding"); //runs first
request.respond();
}
});
}
function getInvites(id, cb) {
var InvitesTable = tables.getTable("Invites").where({
"PlanID": id
}).select("UserID", "Attending");
InvitesTable.read({
success: function (results) {
if (cb) cb(results);
}
});
}
This is a followup question from this as I am unable to use external libraries in Azure. So how can I workaround the problem?
The asynchronous nature of the operations makes that a little challenging. What you need to do is to only call
request.respond()once all the operations are done. I really miss theawaitkeyword which I’m so fond from C#, but I’ve used an “asynchronous for loop” before, and it’s worked quite well. Your code would look something like the one shown below: