Skip to content

Instantly share code, notes, and snippets.

@ptasker
Last active August 29, 2015 14:20
Show Gist options
  • Save ptasker/6f1412a04813f1123157 to your computer and use it in GitHub Desktop.
Save ptasker/6f1412a04813f1123157 to your computer and use it in GitHub Desktop.
NodeJS Promises
function getTweets (req, res) {
var d = Q.defer();
T.get('statuses/user_timeline', {screen_name: 'petetasker', count: 2}, function (err, data, response) {
if (err) {
var error = new Error('Something went wrong trying to get Tweets');
error.innerError = err;
throw error;
}
//return tweet data if no error
d.resolve(data, err, model);
});
//Return a promise
return d.promise;
}
function processTweets( tweets ) {
var d = Q.defer();
var formattedTweets = [];
if (tweets) {
var processed_data = [];
tweets.forEach(function (status) {
processed_data.push(status);
});
}
d.resolve( processed_data );
return d.promise;
}
getTweets(req, res)
.then(processTweets)
.done();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment