javascript - How can I speed up this sequence of promises? -
i have following http request handled in node server. have send list of disks response.
the code is:
diskpromise.getdiskcount(client).then(function (diskcount) { diskpromise.getdisks(client, diskcount).then(function (disks) { raidpromise.getraidcount(client).then(function (raidcount) { raidpromise.getraidarrays(client, raidcount).then(function (raidarrays) { (i in disks) { disks[i].setraidinfo(raidarrays); } raidpromise.getglobalsparelist(client).then (function(sparenames) { (i in disks) { disks[i].setspareness(sparenames); } res.json(disks); }, function (err) { console.log("something (either getdiskcount, or 1 of getdisk calls) blew up", err); res.send(403, { error: err.tostring() }); }); }); }); }); });
the promises soap calls. takes anywhere 4.5 7.0 seconds client response back.
am doing structurally wrong laying out code?
analyzing code implies trivial parallelization opportunities fetching disks, raidarrays , sparenames. performance increased
var disks = diskpromise.getdiskcount(client).then(function (diskcount) { return diskpromise.getdisks(client, diskcount); }); var raidarrays = raidpromise.getraidcount(client).then(function (raidcount) { return raidpromise.getraidarrays(client, raidcount); }); var sparenames = raidpromise.getglobalsparelist(client); promise.all([disks, raidarays, sparenames]).spread(function(disks, raidarrays, sparenames) { for(var in disks) { disks[i].setraidinfo(raidarrays); disks[i].setspareness(sparenames); } res.json(disks); }).catch(function(err) { console.log("something (either getdiskcount, or 1 of getdisk calls) blew up", err); res.send(403, { error: err.tostring() }); });
Comments
Post a Comment