Skip to content

Instantly share code, notes, and snippets.

@ptasker
Created May 7, 2015 15:23
Show Gist options
  • Save ptasker/835e91c7f8f22eee6883 to your computer and use it in GitHub Desktop.
Save ptasker/835e91c7f8f22eee6883 to your computer and use it in GitHub Desktop.
NodeJS Callbacks fixed
var tweets = {
/**
*
* Responsible for running the call to Twitter API
*
* @param err
* @param result
* @returns {*}
*/
doTwitterCall: function (err, result) {
//MOAR code here etc.
//return result
},
/**
*
*
* Query results model (our global model for holding crap...I mean... social data)
*
* @param err
* @param result
*/
getResults: function (err, result) {
this.job_id = this.req.params.jobid;
var that = this;
that.result = result;
this.model2.findOne(
{
'user': this.req.user,
'job_id': job_id,
'social_network_id': 2
})
//
.sort({date: 'asc'})
.exec(this.doTwitterCall.bind(this));
},
kickOffTwitterCall: function (req, res, model1, model2) {
if (!req.user || !req.params.jobid) {
return;
}
//Assign arguments to this, so they're available on nested callbacks
this.req = req;
this.res = res;
this.model1 = model1;
this.model2 = model2;
this.user = req.user;
model1.find({'_id': req.params.jobid}, this.getResults.bind(this));
},
}
tweets.kickOffTwitterCall(req, res, model1, model2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment