0

I have the following code that I made by modifying the listUpcomingEvents() method in this quickstart guide for using the google calendar api.

    function getEvents(starttime,endtime) {
        var request = gapi.client.calendar.events.list({
            'calendarId': 'primary',
            'timeMin': (starttime).toISOString(),
            'timeMax': (endtime).toISOString(),
            'showDeleted': false,
            'singleEvents': true,
            'orderBy': 'startTime'
        });
        request.execute(function(resp) {
            console.log(resp.items);
        });
    }

This prints out an array of the events in the user's calendar between the provided start and end time.

The problem is I want getEvents to return this array rather than printing it. I cannot seem to figure out how to do this though.

I have attempted replacing the last three lines with:

        return request.execute(function(resp) {
            return resp.items;
        });

and:

        var temp;
        request.execute(function(resp) {
            temp = resp.items;
        });
        return temp

but neither of these work. Any tips would be appreciated.

quantumbutterfly
  • 1,815
  • 4
  • 23
  • 38
  • The responses in that thread seem to be geared towards ajax. I don't know enough about javascript to repurpose them to work in my scenario – quantumbutterfly Jul 07 '16 at 22:39
  • 1
    The `$.ajax()` call in the other question is the equivalent of the `request.execute()` call in your question. For structuring your code it doesn't matter what the async function does, it just matters that it does *something* and then *later* calls the callback function that you provided. – nnnnnn Jul 07 '16 at 22:46
  • You cannot **return** *synchronous* result from *asynchronous* function but you can use it in a callback function. – Alex Kudryashev Jul 07 '16 at 22:50
  • P.S. You may find [this more generic async question](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) helpful, particularly [this answer](http://stackoverflow.com/a/23929676/615754), but I think the one we've actually linked your question to is clearer. – nnnnnn Jul 07 '16 at 22:51

0 Answers0