gpt4 book ai didi

javascript - 循环 promise 响应的正确方法

转载 作者:行者123 更新时间:2023-11-30 19:49:08 26 4
gpt4 key购买 nike

我的应用有主要和次要的短信服务。如果主服务失败,对于任何问题,我想默认使用辅助服务。

流程如下:

  1. send to sms service
  2. sms service responds with receipt acknowledgement
  3. sms service sends message to phone carrier
  4. sms service receives phone carrier receipt
  5. my app can then query sms service for carrier receipt

但是,由于所涉及的时间范围(互联网、承运人延迟等),我需要循环 #5 直到承运人收到收据...或超时...以确定是否需要转到二级服务。目前,我在查询 #5 之前进行了 3 秒的超时延迟 - 但我想将其转换为 1 秒的循环,以确保如果 #4 花费的时间超过 3 秒,我的 #5 仍将正常工作。

  $scope.sendCode = function() {
$scope.errColor = "red" ;
$scope.errMsg = "SENDING AUTH CODE" ;
var newCode = getRandom(10000,99999) ;
//var tktCode = new Date().format("yyyymmdd")+ "_" +getRandom(10000,999999) ;
var smsNumber = getDB("user_phone") ;
setDB("app_regCode",newCode) ;
var msg = "App registration code: "+newCode+ ". Enter this code into the 'VERIFY CODE' input field" ;
var tReq = twilio(smsNumber,msg) ;

var smsCarrier = getDB("dev_carrier") ;
if (!smsCarrier) {
smsCarrier = "No Carrier Info" ;
}
var smsEmail = $scope.userObj.user_email ;
if (!smsEmail) {
smsEmail = "No Email" ;
}

$scope.errColor ;
$scope.errMsg ;
var error = {} ;
var smsShortCode = vendorData.twilioShortCode ;
var smsLongCode = vendorData.twilioLongCode ;
// log message sent to internal API
$scope.smsID = logSMS(smsNumber,smsLongCode,smsShortCode,msg,smsCarrier,smsEmail,"Twilio") ;
$http(tReq).then(function(response) {
$scope.errColor = "green" ;
$scope.errMsg = "SENT! Confirming Delivery" ;
smsResponse = response.data ;
if (smsResponse.sid) {
//update smsSent record
apiService.all("PUT",[{table:"smsSent",sid:"rid",sidValue:$scope.smsID,soloField:1,dataInfo:{smsTkt:smsResponse.sid}}]) ;
}
if (smsResponse.status.match(/(queued|accepted|sending|sent)/i)) { //if (sms.success == true) {

// need to replace this with a proper 1 second loop
// to test for "delivered" carrier receipt
// if status still (queued|accepted|sending|sent), then loop until its updated
// final receipt statuses are: Delivered, Undelivered, Failed

setTimeout(function() {
return $http(twilioVerify(smsResponse.uri))
.then(function(response) {
// now check status of message to determine if needed to exit or go to back up service
//return $http(twilioVerify(smsResponse.uri))
if (response.data.status == "delivered") {
$scope.errColor = "green"
$scope.errMsg = "Delivery Confirmed" ;
messageSent('Primary');
} else {
//backup service
// determine fail reason, send to secondary if necessary
messageFail('Primary') ;
$scope.errColor = "red"
$scope.errMsg = "Problem detected, trying secondary service" ;
secondSMS(smsNumber,msg) ;
}
}) ;
},3000) ;

} else { // failed response from Twilio, send to MissionMobile
$scope.errColor = "red" ;
$scope.errMsg = "Problem detected, attempting secondary service"
messageFail('Primary') ;
secondarySMS(smsNumber,msg) ;
}
}, function(thisError) { // failed communication to Twilio, send to MissionMobile
$scope.errColor = "red" ;
$scope.errMsg = "Issue sending code, attempting secondary service" ;
messageFail('Primary') ;
secondarySMS(smsNumber,msg) ;
}).catch(function(error) {
$scope.errColor = error.errColor ;
$scope.errMsg = error.errMsg ;
}) ;
}

最佳答案

如何使用 $interval :

    final_check_interval = $interval(function() {  
return $http(twilioVerify(smsResponse.uri))
.then(function(response) {
// now check status of message to determine if needed to exit or go to back up service
//return $http(twilioVerify(smsResponse.uri))
if (response.data.status == "delivered") {
$scope.errColor = "green"
$scope.errMsg = "Delivery Confirmed" ;
messageSent('Primary');
} else {
//backup service
// determine fail reason, send to secondary if necessary
messageFail('Primary') ;
$scope.errColor = "red"
$scope.errMsg = "Problem detected, trying secondary service" ;
secondSMS(smsNumber,msg) ;
}
}) ;
},1000) ;

messageSent() 上,确保取消每 1 秒触发一次的时间:

$interval.cancel(final_check_interval);

我希望你知道你在找什么

关于javascript - 循环 promise 响应的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54652062/

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