I am new to the Generator
concept. My understanding is that if a function returns a Promise
, then it can be used with yield
. So I have a very small node.js script that looks like this:
Q.fcall(function*(){
var url = "mongodb://" + config.host + ":" + config.port + "/" + config.db;
var db = yield MongoClient.connect( url );
var data = yield makeRequest();
console.log( data );
db.close();
});
function makeRequest(){
var deferred = Q.defer();
request({
"method" : "GET",
"url" : "....",
"headers" : {
"Accept" : "application/json",
"user_key" : "...."
}
},function(err,data){
if( err ){
deferred.reject( err );
}else{
deferred.resolve( data );
}
});
return deferred.promise;
}
I know this works because I am porting it from the callback hell style to generator style. However, I do not see the data in console.log.
What do I need to change to make this work?