Friday 31 January 2020

What exactly promises and async/await are for except for cleaner code?

I've just been getting into node and learning about those things. Is there a really much difference in execution time between three examples below?(This code is just one controller from my project written in three different ways)#1 Regular callback heavy one:exports.po_get = (req, res, next) => { poc.find() .exec(function(err, list) { if (err) { return next(err); } res.render('po.html', { list: list }); }); }; #2 Promises based oneexports.po_get = (req, res, next) => { poc.find() .then((list) => { res.render('po.html', { list: list }); }) .catch(next); }; #3 Async await oneexports.po_get = async (req, res, next) => { try { let list = await poc.find() res.render('po.html', { list: list }); } catch (err) { next(err); } }; How are they different apart from syntax?

Submitted February 01, 2020 at 02:25AM by radekpies

No comments:

Post a Comment