javascript - Node js error handling with callbacks in nested functions -
i know kind of error handling not right, promises should used instead, wondering if work is:
router.get('/main', function(req, res, next) { var mycallback = new function(err, data){ if(err) { res.status(500).send({ error: "error (best handling ever)" }); return; } res.send("success"); return; }; mainprocess(mycallback); }); function mainprocess(callback){ request.post(requestparams, function(error, response, body) { if (error) { console.log(error.message); callback.return(error, ""); } else { request.post(requestparams, function(error, response, body) { if (error) { console.log(error.message); callback.return(error, ""); } else { // success callback.return(null, "success"); } }); } }); }
i test myself, need know whether callback passed parameter used in nested functions , whether it's right approach.
Comments
Post a Comment