I am trying to achieve the following functionality:
- execute call back
- resolve promise
- check output
- if not correct execute again
I have 'mimicked' the scenario with a timer, this reruns a script that makes a call to backend database for some information:
 _runCheckScript: function(bStart, bPreScript){
                var oController = this;
                var scriptTimerdeferred = $.Deferred();
                var promise = scriptTimerdeferred.promise();
                if(typeof(bStart) === "undefined"){
                    bStart = true;
                }
                if(typeof(bPreScript) === "undefined"){
                    bPreScript = true;
                }
                // if the HANA DB is not stopped or started, i.e. it is still starting up or shutting down
                // check the status again every x number of seconds as per the function
                var msTime = 10000;
                if(!bPreScript){
                    this._pushTextIntoConsoleModel("output", {"text":"The instance will be 'pinged' every " + msTime/1000 + " seconds for 2 minutes to monitor for status changes. After this, the script will be terminated."});
                }
                if(bPreScript){
                    var timesRun = 0;
                    var commandTimer = setInterval( function () {
                        timesRun += 1;
                        if(timesRun === 12){
                            scriptTimerdeferred.reject();
                            clearInterval(commandTimer);
                        }
                        // send the deferred to the next function so it can be resolved when finished
                        oController._checkScript(scriptTimerdeferred, bStart, bPreScript);
                    }, msTime);
                }
                return $.Deferred(function() {
                    var dbcheckDeffered = this;
                    promise.done(function () {
                        dbcheckDeffered.resolve();
                        console.log('Check finished');
                            oController._pushTextIntoConsoleModel("output", {"text":"Check finished."});
                    });
                });
The script it calls, has it's own promise as it calls another function:
_checkScript: function(scriptTimerdeferred, bStart, bPreScript){
            var oProperties = this.getView().getModel("configModel");
            var oParams = oProperties.getProperty("/oConfig/oParams");
            var deferred = $.Deferred();
            var promise = deferred.promise();
            var sCompareStatus1 = "inProg";
            var sCompareStatus2 = this._returnHanaCompareStatus(bStart, bPreScript);
            var sCompareStatus3 = this._returnHanaCompareStatus3(bStart, bPreScript);
            var params = {//some params};
            // Send the command
            this._sendAWSCommand(params, deferred);
            // When command is sent
                promise.done(function (oController) {
                    console.log('back to db check script');
                    var oCommandOutputModel = oController.getView().getModel("commandOutput");
                    var sStatus = oCommandOutputModel.Status;
                    // check that it's not in the wrong status for a start/stop
                    // or if it's a pre script check -> pre script checks always resolve first time
                    if(sStatus !== sCompareStatus1 && sStatus !== sCompareStatus2  && sStatus !==sCompareStatus3|| bPreScript){
                        scriptTimerdeferred.resolve();
                    }
                });
        },
This works, however what it does is:
- set a timer to call the first script every x seconds (as the data is currently changing - a server is coming online)
- the script runs and calls another function to get some data from the DB
- when the call for data is resolved (complete) it comes back to 'promise.done' on the checkScript and only resolves the timer promise if it meets certain criteria
- all the while, the initial timer is resending the call as eventually the DB will come online and the status will change
I am wondering if there is a better way to do this as currently I could have, for example, 3 calls to the DB that go unresolved then all resolve at the same time. I would prefer to run a command, wait for it to resolve, check the output, if it is not right then run command again.
Thanks!
 
     
    