0
var geoCorrectionJob = scheduler.scheduleJob('*/30 * * * * *', function(){

//Code section 1
mysqlService.getJoinedMySQLDataWithSuccess().then(function(result) {
    // console.log(result) //will log results.
    var response, attractionId;
    for(var pos = 0; pos < result.length; pos++){
        // attractionid = result[pos].attractionid

        geolocationController.functionUsingPromise(result[pos]).then(function(response) { // `delay` returns a promise
            // console.log(response); // Log the value once it is resolved
            attractionId = response[0];
            if(response[1] == 1){
                attractionsDone.push(attractionId);
                if(attractionsDone.length == config.scheduler.UPDATE_SIZE || pos == result.length - 1){
                    mysqlService.MySQL(attractionsDone).then(function(temp){
                        attractionsDone = [];
                    })
                    .catch((err) => {
                        console.log(err);
                    });;
                }
            }
            else if(response[1] == 0){
                mysqlService.MySql(attractionId);
            }
          })
          .catch((err) => {
            console.log(err);
        });         
    }   

})
.catch((err) => {
    console.log(err);
});

// Code section 2
mysqlService.getProcessStatusOfAttractions().then(function(status){
        // console.log(status);
        var processed = status[0].STATUS;
        var unprocessed = status[1].STATUS;
        var noData = status[2].STATUS;

        emailService.sendEmail(processed, noData, unprocessed);
    })
    .catch((err) => {
        console.log(err);
    });

});

How do I ensure that "Code section 2" is executed after "Code section 1" is completely executed? Code section 1 has a Promisified task within a for loop. I want Code section 2 to be executed after all the tasks in Code section 1 are completed.

1 Answers1

0
var geoCorrectionJob = scheduler.scheduleJob('*/30 * * * * *', function(){
// console.log("Test Job");
var promiseArr = [];
mysqlService.getJoinedMySQLDataWithSuccess().then(function(result) {
    // console.log(result) //will log results.
    var response, attractionId;
    for(var pos = 0; pos < result.length; pos++){
        // attractionid = result[pos].attractionid

        promiseArr.push(new Promise((resolve, reject) => {
            geolocationController.getLatLngWithPromise(result[pos]).then(function(response) { // `delay` returns a promise
            // console.log(response); // Log the value once it is resolved
                attractionId = response[0];
                if(response[1] == 1){
                    attractionsDone.push(attractionId);
                    if(attractionsDone.length == config.scheduler.UPDATE_SIZE || pos == result.length - 1){
                        mysqlService.markAttractionsDoneInMySQL(attractionsDone).then(function(temp){
                            attractionsDone = [];
                            resolve();
                        })
                        .catch((err) => {
                            console.log(err);
                            reject();
                        });;
                    }
                    else{
                        resolve();
                    }
                }
                else if(response[1] == 0){
                    mysqlService.markAttractionNotHavingDataInMySQL(attractionId);
                    resolve();
                }
            })
            .catch((err) => {
                console.log(err);
                reject();
            }); 
        }));        
    }

    Promise.all(promiseArr).then(() => {
        mysqlService.getProcessStatusOfAttractions().then(function(status){
            console.log(status);
            var processed = status[0].STATUS;
            var unprocessed = status[1].STATUS;
            var noData = status[2].STATUS;

            emailService.sendEmail(processed, noData, unprocessed);
        })
        .catch((err) => {
            console.log(err);
        });
    });
})
.catch((err) => {
    console.log(err);
});

console.log(promiseArr); // empty array

});
Akash Dathan
  • 4,348
  • 2
  • 24
  • 45
  • Avoid the [`Promise` constructor antipattern](https://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Feb 06 '18 at 14:06
  • This will hang if `response[1]` is anything other than `0` or `1` – Bergi Feb 06 '18 at 14:07