To move the above code into a promise, you can wrap it in a Promise constructor as follows:

const updateEvent = () => {
  return new Promise((resolve, reject) => {
    calendar.events.update(
      {
        calendarId: 'primary',
        auth: oauth2Client,
        eventId: googleevents,
        resource: updatedEvent,
      },
      (error, result) => {
        if (error) {
          reject(error);
        } else {
          resolve(result);
        }
      }
    );
  });
};

This code creates a function called updateEvent that returns a new Promise. Inside the Promise constructor, the calendar.events.update method is called with the provided parameters. If there is an error, the promise is rejected with the error message. If the update is successful, the promise is resolved with the updated event object.

You can then call this function using .then() and .catch() like this:

updateEvent()
  .then(result => {
    console.log('Event updated successfully:', result);
  })
  .catch(error => {
    console.error('Error updating event:', error);
  });
Convert Google Calendar Event Update to Promise in JavaScript

原文地址: https://www.cveoy.top/t/topic/l3Oz 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录