我正在尝试构建一个包含 2 个外部 API 调用的 Gmail 插件。第一个很快(~200ms),第二个很慢(~5s)。因此,我想首先使用第一次获取的结果构建卡,然后在第二次调用完成后更新卡。
是否有可能:
fetchAll每次请求完成时调用并构建和呈现卡片- 在初始渲染完成后触发一个函数(之后
return card.build()) - 更新根卡不返回(我试过
CardService.newNavigation().popToRoot().updateCard(card.build())没有成功)
任何呈现卡片并在获取数据后对其进行更新的首选方式将不胜感激!
下面是一个有用的示例函数。
function onGmailMessage(e) {
// Fetching email
var messageId = e.gmail.messageId;
var accessToken = e.gmail.accessToken;
GmailApp.setCurrentMessageAccessToken(accessToken);
var message = GmailApp.getMessageById(messageId);
// Preparing requests
var data = {
'text': message.getPlainBody(),
};
var options = {
'method' : 'post',
'contentType': 'application/json',
'payload' : JSON.stringify(data)
};
// Fetching responses. Here I would love to first display
// createCard(response_1) and then when the second call finishes
// return createCard(response_1 + '/n' + response_2)
var response_1 = UrlFetchApp.fetch('http://API_1/', options);
var response_2 = UrlFetchApp.fetch('http://API_2/', options);
return createCard(response_1 + '/n' + response_2);