Wednesday, 15 August 2012

javascript - Change async workflow to Promise (Bluebird) -


I am trying to wrap my head around the promise. I think for basic concepts, but once it is nested, I'm a little confused.

  var getIndividualData = function (url, doneGetIndividualData) {var $, data}  

; Data ({title = $ ("# itemTitle") (request) (url, function (fault, leak, body) {If (! Mess and res.statusCode === 200) {$ = cheerio.load (body ). ("# PrcIsum_bidPrice"). ("# Vi-itm-cond"). Text (), value: $ ("# prcIsum_bidPrice"), text (), imgUrl: $ ("#icImg") [0] .attribs.src, Created: chance dated (), likes: chance.integer ({min: 0, max: 1000})}; doneGetIndividualData (empty, data); } And {DoneGetIndividualData (err);}}); }; Var getListing = function (url, doneGetListing) {var $; Var link = []; Request (url, function (mistake, leak, body) {If (disturbance and res.statusCode === 200} {$ = cheerio.load (body); $ ('. VIP'). I, L) {if (i> lt; 15) {links.push (el.attribs.href);}}); async .concat (link, receivedindia.dataata, function (mistake, result) {refunded GETListing (tap) , Result);});} and {doneGetListing;}}};} var putToMongo = function (fault, result) {if (devConfig.seedDB) {Mongoose connection (devConfig.mongo.uri); Remove .find ({}) (work (mistake, item) {create item (result, work (error, item) {console.log ( 'Done'); process.kill ();}}}};}}; async .concat (url, getListing, putToMongo);

is that there is some promise in many futures libraries to" promise "ASCIN functions, But I do not think it will work here because requests passes through two success values ​​in your callback:

  (request) (reject) () (request) (Reject, reject) {un Contradiction (work (error, leakage, body) {if (err) {reject (mistake);} ({res: res, body: tan}); }); }); };  

Once this happens, it becomes very easy:

  var getIndividualData = function (url) {return request async (url). Then (function (result) {If (result.res.statusCode === 200} {var $ = cheerio.load (result.body); Return {title: $ ("# itemTitle"). Children () ["0 "] Data, Condition: $ (" # vi-ITm-Conduction "). Text (), Value: $ (" #impressi_bidPrice "). Text (), IMGURL: $ (" ICIMG ") [0] .attibs .src, createdAt: chance.date (), like: chance.integer ({min: 0, max: 1000})}} New Error ("Personal Data Status Code:" + result.res.statusCode);};) ;} Get getListing = function (url, doneGetListing) {return requestAsync (url) .then (function (result) {if (result.res.statusCode === 200) {var $ = cheerio.load (result.body) , Promises = $ ('. VIP'). Filters (function (I) {returns i  

No comments:

Post a Comment