Reputation: 263
I am trying to collect results of FB API asynchronous requests which are called in loop. I use next code:
function getPicturesByUserIds(friendsIdList) {
var userPictures = [];
for (var i = 0; i < friendsIdList.length; i++) {
(function () {
var userId = friendsIdList[i];
var j = i;
var friendsIdListLength = friendsIdList.length - 1;
FB.api('/'+userId+'/picture?type=large', function (responce) {
if (responce && !responce.error) {
userPictures[userId] = responce.data.url;
}
if (j >= friendsIdListLength) {
console.log(userPictures);
sendPicturesAndGetResponse(userPictures);
}
});
})();
}
}
This code works in Chrome, but in Firefox array userPictures is empty.
Upvotes: 2
Views: 114
Reputation: 73984
You can solve those kind of things with "recursive functions", or - better - just use one API call for it:
/me/friends?fields=name,picture.type(large)
I would count recursive functions to the basics of programming though, you should get familiar with those. For example: Calling a javascript function recursively
Upvotes: 1