After reading this thread How should I call 3 functions in order to execute them one after the other?, I am still unsure why this doesn't work and having trouble making asynch calls synchronous:
function testCallBack(){
one(two);
}
function one(callback){
setTimeout(function(){alert("delay 1")},2000);
callback();
}
function two(){
setTimeout(function(){alert("delay 2")},1000);
}
How can I make two() wait until one() completes? I still see "delay 2" before I see "delay 1".
EDIT: The above is a simplified example of what I want to do, just trying to do a hello world for callbacks. I am trying to apply it like this:
function add() {
addCalendar(getCalendarID);
}
function addCalendar(callback) {
var req = gapi.client.calendar.calendars.insert(
{
"resource":
{"summary": "McGill Schedule",
"description": "Winter 2015",
"timezone": "Canada/Montreal"}
});
req.execute(function(resp) {
console.log("added calendar");
callback();
});
}
function getCalendarID() {
var req = gapi.client.calendar.calendarList.list({});
req.execute(function(resp) {
for (var i = 0; i < resp.items.length; i++) {
if (resp.items[i].summary === "McGill Schedule") {
console.log("Mcgill Sched id: " + resp.items[i].id);
calendarID = resp.items[i].id;
break;
}
}
});
}
Which still doesn't seem to work even with the callback() inside the response of the api call.