This is the 14th day of my participation in the More text Challenge. For more details, see more text Challenge

This article comes from a long time ago, when I was in the first company and had a communication with my leader at that time.

background

At that time, the leader asked me to create a retry function to execute a certain function. If the function succeeds, the correct result will be returned. If the function fails, the current function needs to be executed 4 times within 1000ms until the correct result is returned.

Think about it in front of the blackboard and write it down. It was a mess.

The initial release

Assuming the random function evaluates to a value greater than 80, we return true, otherwise re-try.

var executeFunction = function() {
    var timespan = 250;
    var maxCount = 4;
    var executeCount = 0;
 
    var success = function() {
        console.log('success');
    }
 
    var fail = function() {
        console.log('fail');
    }
 
    return function returnFunction(callback) {
        setTimeout(function() {
            if (callback()) {
                return success();
            } else if (++executeCount < maxCount) {
                returnFunction(callback);
            } else {
                returnfail(); } }, timespan); }}var testFunction = function() {
    var date = new Date(a);var returnValue = Math.random(0.1) * 100; 
 
    console.log("execute testFunction: Date " + date.toString() + ". ms: " + date.getMilliseconds() );
  	console.info(returnValue)  
  
    if (returnValue > 80) {  
        return true;  
    }   
  
    return false;  
}
 
executeFunction()(testFunction);
Copy the code

Three times, the first two times did not return the correct value, the last time returned the correct value.



【 conjecture 】

Actually, after we wrote the code on the board, we didn’t finish it. The leader said that when I saw you write code, you write code in the process of 1+1=2.

So what does this 1+1=2 mean? When writing code, some non-critical things are written, such as 1000ms, the condition that executes 4 times is written in the code.



As shown in the figure above, we actually analyze this problem in a reverse way. We first consider the problem of comparing the detail, and then execute 1+1 to generate the final code.



The meaning of leader should be as follows

The leader’s idea is let’s do f(x, y), let’s think from the top down.

/*retry*/  
var retry = function(functionName, retryCondition, retryConfig) {  
    if (typeoffunctionName ! ='function') {  
        return "functionName should be function!";  
    }  
  
    if (typeofretryCondition ! ='function') {  
        return "retryCondition should be function!";  
    }  
  
    if (typeofretryConfig ! ='object') {  
        return "retryConfig should be object!";  
    } 
 
 
    retryConfig.retryTimeRange = retryConfig.retryTimeRange || 0;
    retryConfig.retryCount = retryConfig.retryCount || 1;
  
    var returnValue = functionName();  
    var retryIndex = 0;  
  
    if(! retryCondition(returnValue)) {return returnValue;  
    } else {  
        var retryTimeSpan = retryConfig.retryTimeRange / retryConfig.retryCount;  
  
        (function retry() {  
            setTimeout(function() {  
                var returnValue = functionName();  
  
                if(! retryCondition(returnValue)) {/*return true will try*/  
                    return returnValue;  
                } else if (++retryIndex < retryConfig.retryCount) {  
                    retry();  
                } else {  
                    console.info("fail returnvalue: " + returnValue);  
                    returnreturnValue; } }, retryTimeSpan); })(functionName); }}/*test*/  
var functionName1 = function() {  
    var returnValue = Math.random() * 100;  
  
    /*log innfo*/  
    var date = new Date(a);console.info("execute testFunction: Date " + date.toString() + ". ms: " + date.getMilliseconds() );  
    console.info("returnvalue: " + returnValue);  
  
    return returnValue;  
}  
  
var retryCondition1 = function(returnValue) {  
    if (returnValue > 80) {  
        return false;  
    }   
  
    return true;  
}  
  
var retryConfig1 = {  
    retryTimeRange: 1000.retryCount: 5  
}  
  
retry(functionName1, retryCondition1, retryConfig1);  
Copy the code

We look at the returned results (the test here is to use a random number return value, if the random number * 100 > 80 the return value, if the retry time is not the right answer, the return value and mark is the result of a mistake), the following three times to return for the second time is right the return value, the other two times five times retry or no proper return values.



The result of the above is actually the concept of combinatorial functions using functional programming. But it’s not quite the same thing as a combinatorial function. But at the heart of it all is the idea of functional programming.Function composition