0

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.

8
  • What does "completes" mean to you? If you want to wait until after the alert(), then that setTimeout function is wherein you need to put the callback() call. Commented May 18, 2015 at 22:53
  • @Bergi see my edit above - when I run that I dont even see the console.log("Mcgill Sched ...") for some reason. Commented May 18, 2015 at 23:19
  • Can you point me to some documentation for gapi.client.calendar...? Commented May 18, 2015 at 23:34
  • gapi.client is the javascript client for google apis (here: developers.google.com/api-client-library/javascript/start/…), the calendar specific stuff is here developers.google.com/google-apps/calendar/v3/reference Commented May 18, 2015 at 23:41
  • 1
    Never mind it magically starting working! I have this hosted on github pages so it might just have taken a while to update. Thanks for your help. Commented May 18, 2015 at 23:58

1 Answer 1

1

You need to move the callback:

function testCallBack(){
    one(two);
}
function one(callback){
    setTimeout(function(){
        alert("delay 1");
        callback();
    },2000);
}
function two(){
    setTimeout(function(){alert("delay 2")},1000);
}

The point of setTimeout is that it is asynchronous so it wont block your code from continuing. You have the scope to call the callback from within one's setTimeout function though.

See it working in a fiddle

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that makes sense. I still can't seem to apply it to my google calendar api calls though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.