node.js - Why use javascript callbacks when cleaner syntax is available? -
i'm trying learn use node. far, good. but, being pretty new javassript , don't point of using callbacks when cleaner , more readable (at least me) syntax available.
here's example code make point clearer:
with callback:
exports.create = function(req, res){ new todo({ content : req.body.content, updated_at : date.now() }).save(function(err, todo, count){ res.redirect('/'); }); };
without callback:
exports.create = function(req, res){ newtodo = new todo({ content : req.body.content, updated_at : date.now() }); newtodo.save(); res.redirect('/'); };
both of these codes save new todo , redirect.
my preference goes second one, find easier read, maybe there's difference don't get. there difference?
the short answer is: avoid locking user interface while operation takes time finishing executing.
in second example, if save function makes ajax call, have make synchronous ajax call.
Comments
Post a Comment