Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

constantly send a request until a desired response is recieved expressjs

// your callback gets executed automatically once the data is received
var callback = (data, error) => {
    // consume data
    if (error) {
        console.error(error);
        return;
    }
    console.log(data);
};

// run the request. this function will call itself max. 5 times if the request fails
request(5, callback);

function request(var retries, var callback) {
    axios.post('http://localhost:2000/evaluate', {
        serviceName:"s1"
    }).then(response => {
        // request successful

        if(response.data['done'] == 1) {
            // server done, deliver data to script to consume
            callback(response);
        }
        else {
            // server not done yet
            // retry, if any retries left
            if (retries > 0) {
                request(--retries, callback);
            }
            else {
                // no retries left, calling callback with error
                callback([], "out of retries");
            }
        }
    }).catch(error => {
        // ajax error occurred
        // would be better to not retry on 404, 500 and other unrecoverable HTTP errors
        // retry, if any retries left
        if (retries > 0) {
            request(--retries, callback);
        }
        else {
            // no retries left, calling callback with error
            callback([], error);
        }
    });
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: add validation in strapi 
Javascript :: Reactjs exemple function component 
Javascript :: get gravatar javascript 
Javascript :: nodejs express use streams 
Javascript :: poo javascript heritage 
Javascript :: scriptable alert 
Javascript :: tableexport npm 
Javascript :: toast not at bottom 
Javascript :: code ELIFECYCLE npm ERR! errno 126 
Javascript :: jeebisah 
Javascript :: Schalte das jQuery Migrate Script ab 
Javascript :: Logical Assignment Operator null coalescing 
Javascript :: animejs code sample for keyframe property 
Javascript :: multi-line string shorthand javascript 
Javascript :: how to hide all tabs in windows 10 
Javascript :: animated progress bar cdn 
Javascript :: second level relationships data not found in strapi 
Javascript :: string format javascript 
Javascript :: best browser for programmers 
Javascript :: add types to React$Context in flow 
Javascript :: function directory javascript 
Javascript :: “javascript sleep 1 second” is a pretty common code problem that people search ;-) 
Javascript :: Difference b/w AddEventListener and Attach Event 
Javascript :: how to add a key to every html tag in a list react 
Javascript :: get size of json array online 
Javascript :: setinterval clearinterval querySelector until render 
Javascript :: date change 
Javascript :: adding values for metaboxes in wordpress 
Javascript :: pass js variable to css animation 
Javascript :: function sleep(delay) { var start = new Date().getTime(); while (new Date().getTime() < start + delay); } 
ADD CONTENT
Topic
Content
Source link
Name
7+1 =