I have the below code,
var async = require('async'); var rest = require('restler');
async.series([
function(callback){
rest.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json').on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result);
this.retry(5000); // try again after 5 sec
} else {
console.log(result);
} });
callback(null, 'one');
},
function(callback){
console.log('2nd function');
callback(null, 'two');
}
],
// optional callback
function(err, results){
// results is now equal to ['one', 'two']
console.log(results);
});
I am expecting output like this,
{ errors: [ { message: 'Bad Authentication data', code: 215 } ] }
2nd function
[ 'one', 'two' ]
But am getting the below output,
2nd function
[ 'one', 'two' ]
{ errors: [ { message: 'Bad Authentication data', code: 215 } ] }
Rest request output always coming at last. What is the correct way of doing this?
function(callback){
rest.get('https://api.twitter.com/1.1/statuses/mentions_timeline.json').on('complete', function(result) {
if (result instanceof Error) {
console.log('Error:', result);
this.retry(5000); // try again after 5 sec
} else {
console.log(result);
callback(null, 'one');
}
});
},
rest.get
is an asynchronous function. By calling it, you fire it off and continue blindly. Move your callback into rest.get
's callback function.