javascript - Node.js return main loop from inside callback -
this question has answer here:
- how return response asynchronous call? 24 answers
i have function creates object based on db data, , web_based json.
function makeobject(dbdata){ var obj = {}; obj.id = dbdata.id; obj.url = dbdata.url; request(dbdata.url,function(err,res,body){ obj.inventory = json.parse(body).inventory; }); return obj }
this doesn't fill in inventory property (async, etc...) nor work return inside request. know answer pretty basic, can't see it. help,please!
you can either pass in callback argument or return promise. request
has return promise or have promisify in way. callback solution easier going stands.
function makeobject(dbdata, cb) { /* codes */ request(args, function (err, res, body) { obj.inventory = json.parse(body).inventory; cb(err, obj); }); }
then use so
makeobject(dbdata, function (err, obj) { // handle err // things obj });