简体   繁体   中英

How can I speed up this sequence of promises?

I have the following HTTP request handled in my node server. I have to send a list of disks back as a 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) { for (i in disks) { disks[i].setRaidInfo(raidArrays); } RaidPromise.getGlobalSpareList(client).then (function(spareNames) { for (i in disks) { disks[i].setSpareNess(spareNames); } res.json(disks); }, function (err) { console.log("something (either getDiskCount, or one of the getDisk calls) blew up", err); res.send(403, { error: err.toString() }); }); }); }); }); }); 

the promises are SOAP calls. It takes anywhere from 4.5 to 7.0 seconds for the client to get a response back.

Am I doing something structurally wrong laying out the code?

Analyzing your code implies trivial parallelization opportunities for fetching disks, raidArrays and sparenames. Performance greatly 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 i in disks) {
        disks[i].setRaidInfo(raidArrays);
        disks[i].setSpareNess(spareNames);
    }
    res.json(disks);
}).catch(function(err) {
    console.log("something (either getDiskCount, or one of the getDisk calls) blew up", err);
    res.send(403, { error: err.toString() });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM