Callum Slater

Callum Slater

1550039289

Proper way to loop for a promise response

I have a primary and secondary sms service for my app. If the primary fails, for any issue, I want to default to the secondary service.

Here is the flow:

  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

However, due to the time frames involved (internet, carrier delays, etc), I need to loop #5 until the carrier receipt comes in...or times out...to determine if it needs to go to the secondary service. Currently I am doing a 3 second timeout delay before querying for #5 - but I would like to convert that to a 1 second loop to ensure that if #4 takes longer than 3 seconds that my #5 will still working correctly.

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

}


#javascript #angular.js #loops #cordova

What is GEEK

Buddha Community

Randy Rickards

1550046029

How about using $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) ;

and on messageSent(), make sure you cancel the time which will be triggered every 1 secs:

$interval.cancel(final_check_interval);

I hope you got the clue of what you are looking for

Ida  Nader

Ida Nader

1599099600

How to Use If-Else Statements and Loops in R – Dataquest

When we’re programming in R (or any other language, for that matter), we often want to control when and how particular parts of our code are executed. We can do that using control structures like if-else statements, for loops, and while loops.

Control structures are blocks of code that determine how other sections of code are executed based on specified parameters. You can think of these as a bit like the instructions a parent might give a child before leaving the house:

“If I’m not home by 8pm, make yourself dinner.”

Control structures set a condition and tell R what to do when that condition is met or not met. And unlike some kids, R will always do what we tell it to! You can learn more about control structures in the R documentation if you would like.

In this tutorial, we assume you’re familiar with basic data structures, and arithmetic operations in R.

Not quite there yet? Check out our Introductory R Programming course that’s part of our Data Analyst in R path. It’s free to start learning, there are no prerequisites, and there’s nothing to install — you can start learning in your browser right now.

install.packages(“Dataquest”)

Start learning R today with our Introduction to R course — no credit card required!

SIGN UP

(This tutorial is based on our intermediate R programming course, so check that out as well! It’s interactive and will allow you to write and run code right in your browser.)

Comparison Operators in R

In order to use control structures, we need to create statements that will turn out to be either TRUE or FALSE. In the kids example above, the statement “It’s 8pm. Are my parents home yet?” yields TRUE (“Yes”) or FALSE (“No”). In R, the most fundamental way to evaluate something as TRUE or FALSE is through comparison operators.

Below are six essential comparison operators for working with control structures in R:

  • == means equality. The statement x == a framed as a question means “Does the value of x equal the value of a?”
  • != means “not equal”. The statement x == b means “Does the value of x not equal the value of b?”
  • < means “less than”. The statement x < c means “Is the value of x less than the value of c?”
  • <= means “less than or equal”. The statement x <= d means “Is the value of x less or equal to the value of d?”
  • > means “greater than”. The statement x > e means “Is the value of x greater than the value of e?”
  • >= means “greater than or equal”. The statement x >= f means “Is the value of xgreater than or equal to the value of f?”

#data science tutorials #beginner #for loop #for loops #if #if else #learn r #r #r tutorial #rstats #tutorial #tutorials #while loop #while loops

August  Larson

August Larson

1624331040

Python For Loop (with Examples)

A Python tutorial to understand the uses of for loop in various ways including examples.

Python is a general-purpose programming language, which emphasizes making programming easy, efficient coding, and unleashes the user’s potential. Loops are the vital part of programming as it allows the user to repetitive use a set of codes using loops. So in the following article, we will see how to use for loops in python.

FOR Loop:

Till the iteration of the last item in the sequence, for loop run the instructions. It iterates over sets of instructions in sequence, arrays, and a tuple for a pre-defined period or until the last item and calculation are executed.

For loop can be categorized in three ways.

  1. Traditional for loop – It is usually used in programming language and contains three parts of a loop, i.e., initialization, condition, increment/decrement.
  2. Iterator/collection-based for loop – It is used for the iteration of objects and collections instead of numbers.
  3. Vectorize for loop – It is used to iterate parallel arrays simultaneously.

#python #for loop #loops #loop #python for loop

Hudson  Larkin

Hudson Larkin

1599201978

For loop in C++ Program | C++ For Loop Example

For loop in C++ Program | C++ For Loop Example is today’s topic. For understanding for loop, we must have prior knowledge of loops in C++. Loops are used when we want a particular piece of code to run multiple times. We use loops to execute the statement of codes repeatedly until a specific condition is satisfied. It eases the work of the programmer and also shortens the code length.

For loop in C++ Program

For example, if we want to print numbers from 1 to 1000, then if we don’t use loops, we have to write 1000 different print statements for printing numbers from 1 to 1000. With the help of loops, we can write this code in 2 lines. We need to run the loop and give iteration conditions.

There are 3 types of loops for loopwhile loop and do-while loop. In this tutorial, we will learn about for loop.

A for loop is the repetition control structure that is generally used to write a code more efficiently which is supposed to be executed a specific number of times.

#c++ #for loop #while loop #do-while loop

Callum Slater

Callum Slater

1550039289

Proper way to loop for a promise response

I have a primary and secondary sms service for my app. If the primary fails, for any issue, I want to default to the secondary service.

Here is the flow:

  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

However, due to the time frames involved (internet, carrier delays, etc), I need to loop #5 until the carrier receipt comes in...or times out...to determine if it needs to go to the secondary service. Currently I am doing a 3 second timeout delay before querying for #5 - but I would like to convert that to a 1 second loop to ensure that if #4 takes longer than 3 seconds that my #5 will still working correctly.

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

}


#javascript #angular.js #loops #cordova

Promise.allSettled() vs Promise.all()

Promise.allSetlled() is recently introduced in ECMA 2020.
Check out how it is different from Promise.all()

https://www.geekstutorialpoint.com/2020/05/promiseallsettled-vs-promiseall.html

#javascript #promise.all #promise.allsettled #ecma #promise #jquery