javascript - use of async module in node.js -
i got know async module ,and talking it. know below code trigger callback when 2 db calls done.
async.parallel([ function(){ dbcall() }, function(){ dbcall() } ], callback);
but compulsory use async module? if wrap code can async too. wrote in controller
var token = require('../models/token'); token.getalltokens(owner, function(err,callback){ var device_tokens = callback.token; gcm_call(device_tokens); //another ajax call });
above code work, tested it, gcm_call wait , run after getalltokens. why use async module? make code more readable?
so why use async module? make code more readable?
to degree, yes, make more readable. provides useful utilities write more readable , more performant code.
but importantly, because it's solving common problems when using async functionality. showing example used single callback, async module example in first code block first waiting async stuff finish, calls callback function. how solve vanilla js? if know promise api, might promise.all([promise1, promise2, ...])
. when want use async.series
, single task should ran @ time? that's case you'll see hacked solutions, became known callback hell, callbacks nested 1 another.
so create vanilla code solutions use of promise api , simpler adding use of async
/await
future ecmascript spec, library saving hassle of repeating every time occur problem described above.
Comments
Post a Comment