使用异步处理来自 Google 日历 API 的承诺
Using async to handle promise from Google Calendar API
我正在努力处理对 Google 日历 API 的调用。我根据需要从 Google API 文档中稍微修改了 Node.js 快速入门。我正在尝试从日历中检索所有事件,但是当我尝试等待 return 时,我得到的只是 Promise { <pending> }
。有没有更好的方法来等待电话,或者我完全错过了什么?
async function listEvents() {
const calendar = google.calendar({version: 'v3', auth: apiKey});
return await calendar.events.list({
calendarId: <calendarID>,
timeMin: oneWeekAgo,
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const events = res.data.items;
if (events.length) {
events.forEach(el => console.log(el.summary))
// console.log(events)
// eventList = events
return events
} else {
console.log('No upcoming events found.');
}
});
}
FWIW,事件最终会记录到控制台,我只是不知道如何让函数在 returning 之前等待该承诺解决。
这是我用来获取日历事件的 TypeScript 代码
let results: GaxiosResponse<calendar_v3.Schema$Events>;
const maxResults: any = process.env.MAX_CALENDAR_RESULTS;
try {
this.logger.info('Fetching events for primary calendar id');
results = await gcalClient.events.list({
calendarId: 'primary',
timeMin: (new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString()),
maxResults,
singleEvents: true,
orderBy: 'startTime'
});
this.logger.info('Successfully fetched calendar events');
} catch (err) {
this.logger.error('Error encountered while fetching events');
this.logger.error(err);
return err;
}
// Extract the Calendar Events from the response received
const eventList: calendar_v3.Schema$Event[] = results.data.items;
您可以在 async
函数和 return eventList
中添加它,这将使函数的 return 类型成为 Promise<calendar_v3.Schema$Event[]>
现在,无论您在哪里调用该函数,都可以使用 await
或 .then()
获取日历事件
您正在尝试将 await 与回调样式一起使用。我想如果你只是删除回调函数并等待它 return 你会得到详细信息
async function listEvents() {
const calendar = google.calendar({version: 'v3', auth: apiKey});
const res = await calendar.events.list({
calendarId: <calendarID>,
timeMin: oneWeekAgo,
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
});
const events = res.data.items;
if (events.length) {
events.forEach(el => console.log(el.summary))
// console.log(events)
// eventList = events
return events
} else {
console.log('No upcoming events found.');
}
}
我正在努力处理对 Google 日历 API 的调用。我根据需要从 Google API 文档中稍微修改了 Node.js 快速入门。我正在尝试从日历中检索所有事件,但是当我尝试等待 return 时,我得到的只是 Promise { <pending> }
。有没有更好的方法来等待电话,或者我完全错过了什么?
async function listEvents() {
const calendar = google.calendar({version: 'v3', auth: apiKey});
return await calendar.events.list({
calendarId: <calendarID>,
timeMin: oneWeekAgo,
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
}, (err, res) => {
if (err) return console.log('The API returned an error: ' + err);
const events = res.data.items;
if (events.length) {
events.forEach(el => console.log(el.summary))
// console.log(events)
// eventList = events
return events
} else {
console.log('No upcoming events found.');
}
});
}
FWIW,事件最终会记录到控制台,我只是不知道如何让函数在 returning 之前等待该承诺解决。
这是我用来获取日历事件的 TypeScript 代码
let results: GaxiosResponse<calendar_v3.Schema$Events>;
const maxResults: any = process.env.MAX_CALENDAR_RESULTS;
try {
this.logger.info('Fetching events for primary calendar id');
results = await gcalClient.events.list({
calendarId: 'primary',
timeMin: (new Date(Date.now() - 30 * 24 * 60 * 60 * 1000).toISOString()),
maxResults,
singleEvents: true,
orderBy: 'startTime'
});
this.logger.info('Successfully fetched calendar events');
} catch (err) {
this.logger.error('Error encountered while fetching events');
this.logger.error(err);
return err;
}
// Extract the Calendar Events from the response received
const eventList: calendar_v3.Schema$Event[] = results.data.items;
您可以在 async
函数和 return eventList
中添加它,这将使函数的 return 类型成为 Promise<calendar_v3.Schema$Event[]>
现在,无论您在哪里调用该函数,都可以使用 await
或 .then()
获取日历事件
您正在尝试将 await 与回调样式一起使用。我想如果你只是删除回调函数并等待它 return 你会得到详细信息
async function listEvents() {
const calendar = google.calendar({version: 'v3', auth: apiKey});
const res = await calendar.events.list({
calendarId: <calendarID>,
timeMin: oneWeekAgo,
maxResults: 10,
singleEvents: true,
orderBy: 'startTime',
});
const events = res.data.items;
if (events.length) {
events.forEach(el => console.log(el.summary))
// console.log(events)
// eventList = events
return events
} else {
console.log('No upcoming events found.');
}
}