I am trying to use the twitter API to get search results and then modify them, before displaying them on the page.
I am new to the idea of Asynchronous functions and I dont see how to run this method for several search strings and work on the results:
var results = [];
for (string in searches) {
client.get('search/tweets', {q: searches[string]}, function(error, tweets, response){
console.log(tweets); // I need to put these in an array (results) and get the next lot
});
}
analyse(results);
I need to run search/tweets several times and build up an array of the results so they can be analysed. I dont know how to do this if I have to work in the function? Putting it into a callback function would have the same problem.
4 Answers 4
For things like this I like to use Promises. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
let promises = [];
// for each string you have create a new promise and put it in an array
for (string in searches) {
promises.push(new Promise((resolve, reject) => {
client.get('search/tweets', {q: searches[string]}, function(error, tweets, response){
resolve(tweets);
});
}));
}
// after your loop use Promise.all to wait until all promises are resolved and then do something with the results
Promise.all(promises).then(results => {
console.log(results);
});
Comments
The basic idea is this: keep track of how many search terms you are querying, and process the analysis when the last one finishes.
var results = [];
var count = 0;
for (s in searches) {
client.get('search/tweets', {q: searches[string]}, function(error, tweets, response){
console.log(tweets);
results += tweets;
if (++count == searches.count) analyse(results);
});
}
There are also more "modern" ways of handling async code including promises, which I encourage you to check out in your async endeavours.
Comments
There are basically two working parts here. (1) Running a function on an interval and (2) making an async call.
Run a function at an interval
The first is the ability to run a function at an interval. This can be accomplished the following way.
var myInterval = setInterval(function() {
// do something here every second
}, 1000);
Make an Async Call
The second is making an asynchronous call. An asynchronous call is asynchronous because the result cannot be guaranteed to exist at the time of execution. You must perform any analysis/data manipulation inside of the async call. This is the only way to guarantee a response.
Assuming, based on your example, that client.get() is the asynchronous call, you would do something like this:
client.get('search/tweets', ..., function(err, tweets, response) {
// perform some actions on the result
analyse(tweets);
});
Putting it all together:
// make a request every second
var myInterval = setInterval(function() {
// perform async operation
client.get('search/tweets', ..., function(err, tweets, response) {
// perform operations on result
analyse(tweets);
});
}, 1000);
function analyse(tweets) {
// do something with data here
});
Comments
It sounds like you're wanting these requests to work in serial(executing one request at a time)... In pure JS, it would go something like:
var results=[], i=0, l=searches.length;
var next=function(callback){
if(i<l)
{
client.get("search/tweets", {q:searches[i]}, function(err, tweets, response){
results.push(tweets);
next(callback);
});
}
else
{
callback(results);
}
};
next(function(results){
analyze(results);
});
However, these days, as noted by realseanp, Promises make this way cleaner(although his answer would run the requests in parallel).