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

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -