2

How to scope datat ? Here datat is empty. And i would like to put data in a var so i can use it outside the function.

var datat;
twit.search('#louieck', {include_entities:true,page:paget,maxid:maxidt}, function(data) {

// and output to the console:

datat =  data;

});
sys.puts(sys.inspect(datat));

Regards

Bussiere

1
  • At the time you call sys.puts, the callback function hasn't been called yet so datat is empty. Welcome to the world of asynchronicity :) Commented Mar 22, 2013 at 12:29

1 Answer 1

2

'datat' is scoped outside your function. twit.search is async and therefore may not return 'data' before you check 'datat' with sys.inspect.

This should let you see datat:

var datat;
twit.search('#louieck', {include_entities:true,page:paget,maxid:maxidt}, function(data) {

    // and output to the console:
    datat =  data;
    sys.puts(sys.inspect(datat));

});

But ideally you'd use a callback like this...

var datat;
var callback = function(d){
    sys.puts(sys.inspect(d));

    datat = d;
    // do something more with datat

};

twit.search('#louieck', {include_entities:true,page:paget,maxid:maxidt}, function(data) {

    callback(data);

});

EDIT - simplified as per comment...

var datat;
var callback = function(d){
  sys.puts(sys.inspect(d));
  datat = d;
  // do something more with datat
};
twit.search('#louieck', {include_entities:true,page:paget,maxid:maxidt},callback(data));
Sign up to request clarification or add additional context in comments.

1 Comment

In the second example, you don't need the anonymous function. Just pass callback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.