I have a function that takes, as an argument, the response from the gapi.client.calendar.events.list
call on the Google Calendar API.
我有一个函数,它接受来自Google Calendar API上的gapi.client.calendar.events.list调用的响应作为参数。
I'm trying to iterate through that response, and .push()
the elements to an array (localEventList
). When I check the value of localEventList
immediately after the for-loop, the events are stored in it, but when I check it outside of .execute(function(events){...
function, the array is still empty. I've tried initializing the array to different values, and the array remains at the value it is initialized to.
我正在尝试迭代该响应,并将.push()元素转换为数组(localEventList)。当我在for循环之后立即检查localEventList的值时,事件存储在其中,但是当我在.execute(function(events){...函数)之外检查它时,数组仍然是空的。我已经尝试将数组初始化为不同的值,并且数组保持其初始化的值。
I don't understand why the variable isn't staying mutated, since it's initialized in the overarching function call. Also, I'm not really clear on the purpose of the .execute(...
call.
我不明白为什么变量不会保持变异,因为它是在总体函数调用中初始化的。另外,我不太清楚.execute的目的(...调用。
If anyone could clarify either of these two issues, I'd love you forever.
如果有人能澄清这两个问题中的任何一个,我会永远爱你。
function parseEvents(calEventRequest){
var localEventList = new Array();
calEventRequest.execute(function(events){
for(i = 0; i < events.items.length; i++){
var item = events.items[i];
var name = item.summary;
var start = item.dateTime;
localEventList.push(name);
};
// If I place a console.log(localEventList) here, the array is populated
});
console.log(localEventList); // But the call here displays an empty array.
}
1 个解决方案
#1
0
The simple answer to your question is console.log()
will not wait for the calEventRequest.execute()
function to be completed, if you are working with angularJs or any other js framework you can handle this operation with defer
and promise
like features which will wait for the api call and return the response only after finish.
你的问题的简单答案是console.log()不会等待calEventRequest.execute()函数完成,如果你正在使用angularJs或任何其他js框架,你可以使用延迟和承诺的功能来处理这个操作将等待api调用并在完成后返回响应。
the solution to your problem is to use a callback
function after the forloop as below
问题的解决方案是在forloop之后使用回调函数,如下所示
// calling the parseEvents function
parseEvents(calEventRequest,function(eventList){
console.log(eventList);
//this will print the array after for loop finished
})
function parseEvents(calEventRequest,callback){
var localEventList = new Array();
calEventRequest.execute(function(events){
for(i = 0; i < events.items.length; i++){
var item = events.items[i];
var name = item.summary;
var start = item.dateTime;
localEventList.push(name);
};
if(callback)callback(localEventList);
// If I place a console.log(localEventList) here, the array is populated
});
console.log(localEventList); // But the call here displays an empty array.
}
#1
0
The simple answer to your question is console.log()
will not wait for the calEventRequest.execute()
function to be completed, if you are working with angularJs or any other js framework you can handle this operation with defer
and promise
like features which will wait for the api call and return the response only after finish.
你的问题的简单答案是console.log()不会等待calEventRequest.execute()函数完成,如果你正在使用angularJs或任何其他js框架,你可以使用延迟和承诺的功能来处理这个操作将等待api调用并在完成后返回响应。
the solution to your problem is to use a callback
function after the forloop as below
问题的解决方案是在forloop之后使用回调函数,如下所示
// calling the parseEvents function
parseEvents(calEventRequest,function(eventList){
console.log(eventList);
//this will print the array after for loop finished
})
function parseEvents(calEventRequest,callback){
var localEventList = new Array();
calEventRequest.execute(function(events){
for(i = 0; i < events.items.length; i++){
var item = events.items[i];
var name = item.summary;
var start = item.dateTime;
localEventList.push(name);
};
if(callback)callback(localEventList);
// If I place a console.log(localEventList) here, the array is populated
});
console.log(localEventList); // But the call here displays an empty array.
}