harsh Posted January 15, 2022 Report Share Posted January 15, 2022 why vac_friends list is empty when I print at last ? function checkVacID(){ friend_list = friends; vac_friends = [] friend_list.forEach(function(key, index){ axios.get(`https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=&steamids=${key}`) .then(function (response) { vac_response = response.data; if(vac_response.players[0]['VACBanned'] == true){ vac_friends.push(vac_response.players[0]) console.log(vac_friends) } }) .catch(function (error) { console.log(error); }) }); console.log(vac_friends) } Quote Link to comment Share on other sites More sharing options...
Dr. McKay Posted January 15, 2022 Report Share Posted January 15, 2022 Because you're mixing up async and sync. forEach returns before your requests complete. Also, rather than looping through the friend list, you should split it into chunks of 100 and make one request per chunk, to avoid getting rate limited by the API. Quote Link to comment Share on other sites More sharing options...
harsh Posted January 24, 2022 Author Report Share Posted January 24, 2022 On 1/16/2022 at 4:05 AM, Dr. McKay said: Because you're mixing up async and sync. forEach returns before your requests complete. Also, rather than looping through the friend list, you should split it into chunks of 100 and make one request per chunk, to avoid getting rate limited by the API. can you give an example of 100 steamids passing in api like i have 256 friends Quote Link to comment Share on other sites More sharing options...
Dr. McKay Posted February 2, 2022 Report Share Posted February 2, 2022 let friendsList = [array of steamids]; let chunks = []; for (let i = 0; i < friendsList.length; i += 100) { chunks.push(friendsList.slice(i, i + 100)); } let chunkResults = await Promise.all(chunks.map(ck => new Promise((resolve, reject) => { try { let result = await axios.get(`https://api.steampowered.com/ISteamUser/GetPlayerBans/v1/?key=&steamids=${ck.join(',')}`); if (result && result.data && result.data.players) { return resolve(result.data.players); } reject(new Error('Malformed API response')); } catch (ex) { reject(ex); } }))); let vacFriends = []; chunkResults.forEach((chunkPlayers) => { chunkPlayers.filter(player => player.VACBanned).forEach((bannedPlayer) => { vacFriends.push(bannedPlayer); }); }); Untested and might have bugs SENPAY98K 1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.