1

I'm not sure how to get return value from the below code. I want to get the return value from authorizeAndListEventsNew method which was obtained from createEvent.

How should i get the return value promises within the promises, I tried using async and await along with promises but dosent work out.

 function authorizeAndListEventsNew(createEvent,name,d,loc,mobile){
        return fs.readFile("credentials.json",(err,content)=>{
            if(err) return console.log('Error loading client secret file',err);
            const {client_secret, client_id, javascript_origins} = credentials.web;
            const oAuth2Client = new google.auth.OAuth2(
                client_id, client_secret, javascript_origins);
            return fs.readFile(TOKEN_PATH, (err, token) => {
                if (err) return getAccessToken(oAuth2Client, callback);
                oAuth2Client.setCredentials(JSON.parse(token));
                callback(oAuth2Client,name,d,loc,mobile);
            });
    
        });
    }
function createEvent(auth,pname,std,loc,desc){
    const calendar = google.calendar({version: 'v3', auth});
    listUpcommingEvents(calendar,std).then((res)=>{
        var newDt=new Date(std);
        if(res.length>0){
            var sortEvts = res.sort((a,b)=> a - b);
            var lastEle = sortEvts.slice(-1).pop();
            newDt.setTime(lastEle.getTime());
            
        } else {
            newDt.setHours(09,0,0);
        }
        var endTime = new Date();
        var d = new Date(newDt);
        var endTime = moment(d).add(10,"m").toDate();
        var event = {
            'summary': pname,
            'location': loc,
            'description': desc,
            'start': {
                'dateTime': d.toISOString(),
                'timeZone': 'xxx'
            },
            'end': {
                'dateTime': endTime.toISOString(),
                'timeZone': 'xxx'
            },
            
            };
            calendar.events.insert({
                auth: auth,
                calendarId: CALENDAR_ID,
                resource: event,
              }, function(err, event) {
                if (err) {
                  console.log('There was an error contacting the Calendar service: ' + err);
                  return true;
                }
                console.log('Event created: %s', event);
                return false;
              });
    });
};
  • Does this answer your question? [return value after a promise](https://stackoverflow.com/questions/22951208/return-value-after-a-promise) – Linda Lawton - DaImTo Mar 08 '21 at 08:28
  • I'm not able to see where you made use of async-await. However, you can't "return" directly a promise since you will have to resolve the thenable function returned in `authorizeAndListEventsNew`, have you taken a look to the [Calendar Quickstart](https://developers.google.com/calendar/quickstart/nodejs) ? – Jose Vasquez Mar 09 '21 at 09:07

0 Answers0