gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:22:32 29 4
gpt4 key购买 nike

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

  • 执行回调
  • 兑现 promise
  • 检查输出
  • 如果不正确则再次执行

我用计时器“模仿”了该场景,这会重新运行一个脚本,该脚本调用后端数据库以获取某些信息:

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

});

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

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

});
},

这可行,但它的作用是:

  • 设置一个计时器,每 x 秒调用第一个脚本(因为数据当前正在变化 - 服务器即将上线)
  • 脚本运行并调用另一个函数以从数据库获取一些数据
  • 当数据调用得到解决(完成)时,它会返回到 checkScript 上的“promise.done”,并且仅在满足特定条件时才解决计时器 promise
  • 始终,初始计时器正在重新发送调用,因为最终数据库将上线并且状态将发生变化

我想知道是否有更好的方法来做到这一点,例如,目前我可以有 3 个对数据库的调用未解决,然后全部同时解决。我更愿意运行一个命令,等待它解析,检查输出,如果不正确,则再次运行命令。

谢谢!

最佳答案

我认为您想要做的事情可以通过仔细阅读这些链接中的解释来实现:

Promise Retry Design Patterns

In javascript, a function which returns promise and retries the inner async process best practice

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

由于条件不满足,它会无限次重试 promise 。您的条件是您所说的“检查输出”。如果您的检查失败,请重试 promise 。 # 小心保持极限情况, promise 浪费内存。如果你的 api/service/server/callreceiver 关闭,并且你没有设置阈值,你可以创建一个无限的 promise 链,NO STOP

关于javascript - 延迟 promise - 如果回调不符合条件,则重新发送相同的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43879899/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com