在完全执行另一段代码后,如何确保执行特定代码?有或没有Promises的帮助

时间:2018-02-06 10:19:46

标签: node.js promise

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);
    });

});

我如何确保&#34;代码第2节&#34;在&#34;代码部分1&#34;之后执行完全执行了?代码部分1在for循环中具有Promisified任务。我希望在代码部分1中的所有任务完成后执行代码部分2。

1 个答案:

答案 0 :(得分: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

});