Thursday 20 April 2017

Promise.prototype.then() behavior

I have this promise:

var seasonalityArray;   

    ...

aService.gettingAnalysis(site, cohortKey)
            .then(function (result) {
                let date = moment();
                seasonalityArray = result.cooling.getSeasonMonths(date);
            })
            , function (error) {
                console.error('An error occurred', error);
            };


const totalAccounts = _.size(report.asModel().accountNumbers);

const warnings = _.compact(_.map(seasonalityArray, function (monthLabel) {

               ...

}));

Inside seasonalityArray variable, it must get an array of months (e.g. ['June','July','August']) which will be used afterwards.

The problem that I found while debugging each step is that after it goes in the line with aService.gettingAnalysis it doesn't enter in .then() and all the code after but it jumps to setting totalAccounts and afterwards it enters in warnings with seasonalityArray being undefined.

Is there a way to make it enter inside then() or at least to set that variable before it is using it as undefined?



via migea

No comments:

Post a Comment