【发布时间】:2021-04-15 19:46:45
【问题描述】:
是否可以从嵌套函数返回值?这是我到目前为止得到的,它不起作用。在我的代码中,我试图从request.execute() 函数中返回eventId。我不知道我是否朝着正确的方向前进。任何帮助将不胜感激。
function insertDate(dateFormat) {
var eventId = "";
gapi.client.load('calendar', 'v3', function() { // load the calendar api (version 3)
var request = gapi.client.calendar.events.insert({
'calendarId': 'primary', // calendar ID
"sendNotifications": true,
"resource": dateFormat // pass event details with api call
});
// handle the response from our api call
request.execute(function(resp) {
if(resp.status=='confirmed') {
eventId = resp.id
console.log("eventId in execute: " + eventId)
console.log("successfully inserted")
return eventId
} else {
console.log("failed to insert")
}
console.log(resp);
console.log("2: " + resp.id);
});
return eventId
});
console.log("eventId on return: " + eventId)
return eventId
}
我将函数 insertDate(dateFormat) 放入一个循环中,并将我试图从 request.execute() 函数中获取的 eventId 添加到我的 eventIdArray 中,如下所示
var eventIdArray = [];
eventIdArray[i] = insertDate(dateFormat);
console.log("eventIdArray: " + eventIdArray[i]);
【问题讨论】:
标签: javascript function return-value nested-function