简体   繁体   中英

Bluebird promises in loop

I have given a task to revive old javascript code. New system uses bluebird promises. I have made a bit of changes to use them, but I got at this point, and I am not sure, if the way I am going to realize it, will be the best one.

Here I will show a snippet of the old code:

[...] // X0
.then(function(){
    [...] //X1

    // XXX
    if (assesment.data.AssesmentType == "1" || assesment.data.AssesmentType == "4") {
        for (var i = 0; i < assesment.data.Compartments.length; i++) {
            for (var b = 0; b < assesment.data.Compartments[i].Species.length; b++) {
                assesment.assortmentRow(assesment.data.Compartments[i].Species[b], false);
                // assesment.assortmentRow makes ajax request and does other studd
                // and now returns a promise
            }
        }
    }
    // X2
    if (assesment.data.AssesmentType != "2") {
        var asortRowRadioBtn = $("#assesment-assortment-compartments input[type='radio']").eq(0);
        asortRowRadioBtn.prop("checked", true);
        asortRowRadioBtn.trigger("click");
    }
    [...] //X3
});

What this code does actually is not important, for you to help. I have just added a comments, just to label blocks of code here (with X0 , X1 , XXX , etc)

In the old code, all those blocks were one after other. I had to split them, to use them with promises somehow.

So the question is - what to do with blob XXX

One thing I can think of -

[...] // X0
.then(function(){
    [...] //X1
    // new XXX 
    if (assesment.data.AssesmentType == "1" || assesment.data.AssesmentType == "4") {
        return Promise.each(assesment.data.Compartments, function(i) {
            return Promise.each(assesment.data.Compartments[i].Species, function(b) {
                return assesment.assortmentRow(assesment.data.Compartments[i].Species[b], false);
            });
        });
    }
     //X2 and X3 blobs wont be accessible here. 
     //So should I bring them inside next .then() function ? 
     //And is it OK for X1 blob and XXX to be in the same then()?
})
.then(function() ... 

Your code is almost ready - you just need to put the code where it says X2 and X3 blobs won't be accessible here to a next then in the chain - since the Promise.each is (rightfully) in a return it won't reach that area.

[...] // X0
.then(function(){
    [...] //X1
    // new XXX 
    if (assesment.data.AssesmentType == "1" || assesment.data.AssesmentType == "4") {
        return Promise.each(assesment.data.Compartments, function(comp) {
            return Promise.each(comp.Species, function(b) {
                return assesment.assortmentRow(b, false);
            });
        });
    }
})
.then(function(val ) { 
    // x2 and x3 code parts go here, 
});

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