Skip to content

Instantly share code, notes, and snippets.

@1dolinski
Last active January 1, 2016 23:09
Show Gist options
  • Save 1dolinski/8214553 to your computer and use it in GitHub Desktop.
Save 1dolinski/8214553 to your computer and use it in GitHub Desktop.
//////////////// this is twit_pull.js
function TWEET(username, count){
T.get('statuses/home_timeline', { count: count, screen_name: username, trim_user: true, exclude_replies: true }, function (err, reply) {
reutrn reply;
});
}
exports.get_tweets = TWEET('microsoft', 3)
//////////////////
// this is index.js which will be used for an express routing file
var twit = require("../twit_pull.js");
exports.index = function(req, res){
res.render('index',
{ title: 'tweet', cool: twit.get_tweets });
};

butblack: the problem is that T.get is very likely executed asynchronously and that is why you must pass it a callback function. you can't return values from that callback function because it'll sometime later from the point of view of the code that comes after it.

butblack: you do what ever you have to do in the callback. that function is executed when the data you want (the reply) exists. forget about returning the data, just use it

butblack: think of async functions like depositing a check into an empty account. you can't use the money until the check has cleared. what you are trying to do is deposit a check and then withdraw money immediately

butblack: at some point you were going to do var tweet = TWEET( ... ) right? then you were going to use that tweet for something right afterwards, no?

butblack: take (almost) everything you were going to do right after that TWEET call and stick it into a function - function afterTWEET(tweet){ .. } and from your callback do afterTWEET(reply) [10:32PM] ImBcmDth: butblack: ..that is the basic idea

// from: https://github.com/ttezel/twit
// the T object has been setup properly and I'm at this object function..
T.post('statuses/update', { status: 'hello world!' }, function(err, reply) {
// ...
})
// Question: how can I put this into a function such that the function returns reply?
// I tried:
function TWEET(username, count,){
T.get('statuses/home_timeline', { count: count, screen_name: username, trim_user: true, exclude_replies: true }, function (err, reply) {
return reply;
});
}
// but this doesn't return anything, I also tried to put return in front of the T.get portion.. it still does nothing
@imbcmdth
Copy link

imbcmdth commented Jan 2, 2014

//////////////// this is twit_pull.js

function TWEET(username, count, callback){
  T.get('statuses/home_timeline', { count: count, screen_name: username, trim_user: true, exclude_replies: true }, callback);
}

// this bind is not strictly necessary but it fits with what you were trying to do...
exports.getMSTweets = TWEET.bind(null, 'microsoft', 3)

//////////////////

// this is index.js which will be used for an express routing file

var twit = require("../twit_pull.js");

exports.index = function(req, res){
    twit.getMSTweets(function(err, data) {
        res.render('index',
            { title: 'tweet', cool: data });
    });
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment