1

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) {

               ...

}));

...

   aService.gettingAnalysis = function (site, Key) {
        return Promise.all([
            aService.findingHeat(site, Key),
            aService.findingCool(site, Key)
        ]).spread(function (heating, cooling) {
            return { heating: heating, cooling: cooling };
        });
    };

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?

Samurai Jack
  • 2,985
  • 8
  • 35
  • 58
  • you need to explain the nature of aService.gettingAnalysis(..) more - could you post the code up for that, or maybe add a JSFiddle ? – user2808054 Apr 20 '17 at 14:03
  • 3
    Also, the formatting of this code is incorrect - this would likely produce some strange errors. Furthermore, this isn't the way promises work. Promises are async as in synchronous code execution will still apply - there will be no blocking of code therefore the `seasonalityArray` will always be undefined. – Henrik Andersson Apr 20 '17 at 14:04
  • 1
    umm.. could you update the question text ? it'll be much clearer that way. thanks – user2808054 Apr 20 '17 at 14:09
  • repeat this mantra ... *Promises do not make asynchronous code synchronous* ... – Jaromanda X Apr 21 '17 at 03:11

2 Answers2

2
aService.gettingAnalysis(site, cohortKey)
        .then(function (result) {
            let date = moment();
            seasonalityArray = result.cooling.getSeasonMonths(date);
            //now we can do the rest
            const totalAccounts = _.size(report.asModel().accountNumbers);

            const warnings = _.compact(_.map(seasonalityArray, function (monthLabel) {}));
        })
        , function (error) {
            console.error('An error occurred', error);
        };

Possible duplicate of how to return from an asynchronous call, so you may read a bit more about Promises. A Promise is not meant to execute right now, but somewhen. So you cannot simply put code behind it and expect it to execute after the Promise has finished. And you may have a look at async && await ...

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
2

If you need the data to be there when you use it you have to use it after your promise resolves.You can move the work you need to be done to a function.

var seasonalityArray;

...

aService.gettingAnalysis(site, cohortKey)
    .then(function(result) {
        let date = moment();
        seasonalityArray = result.cooling.getSeasonMonths(date);
        doStuff();
    })

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

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

}

I'd do away with the "global" and chain promises instead.

aService.gettingAnalysis(site, cohortKey)
    .then(function(result) {
        let date = moment();
        seasonalityArray = result.cooling.getSeasonMonths(date);
        return seasonalityArray;
    }).then(doStuff)

function doStuff(seasonalityArray) {
    const totalAccounts = _.size(report.asModel().accountNumbers);

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

}

then returns a promise where the data that get's resolved is what the then returns.