I'm trying to fetch data from array every two seconds and then send the request to the twitter API to follow the user. The problem is that after I run the script it instantly throws me all 400 users and then after two seconds another one is being thrown. How can I fix this so I see every user followed after two seconds and not see all those 400 thrown at the script start?
const params = {screen_name: 'user'};
client.get('friends/ids', params, function(error, tweets, response) {
if (!error) {
const body = JSON.parse(response.body);
body.ids.slice(0, 400).forEach((element) =>
setInterval( () => {
client.post('friendships/create', {user_id: element}, function(error, tweets, response){
console.log(`User ${element} followed.`);
})
}, 2000)
);
}
});
first-getflag of similar.