延迟承诺 - 如果回调不符合条件,则重新发送相同的请求

Deferred Promise - Resend same request if callback doesn't meet criteria

我正在尝试实现以下功能:

我有 'mimicked' 带有计时器的场景,这重新运行是一个调用后端数据库以获取某些信息的脚本:

 _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."});
                    });

                });

它调用的脚本在调用另一个函数时有自己的承诺:

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

                });
        },

这有效,但它的作用是:

我想知道是否有更好的方法来执行此操作,例如,我可以有 3 个未解决的数据库调用,然后同时解决所有问题。我更喜欢 运行 一个命令,等待它解决,检查输出,如果不正确然后再次 运行 命令。

谢谢!

我认为你想做的事情可以实现,仔细阅读这些链接中的解释:

See this jsfiddle

var max = 5;

var p = Promise.reject();
for(var i=0; i<max; i++) {
    p = p.catch(attempt).then(test);
}
p = p.then(processResult).catch(errorHandler);

function attempt() {
    var rand = Math.random();
    if(rand < 0.8) {
        throw rand;
    } else {
        return rand;
    }
}
function test(val) {
    if(val < 0.9) {
        throw val;
    } else {
        return val;
    }
}
function processResult(res) {
    console.log(res);
}
function errorHandler(err) {
    console.error(err);
}

由于条件不满足,它会无限次重试承诺。你的条件就是你说的点"check the output"。如果检查失败,请重试承诺。 # 小心持有一个极限情况,承诺浪费内存。如果你的 api/service/server/callreceiver 关闭,并且你没有设置阈值,你可以创建一个无限的承诺链 NO STOP