terus -menerus mengirim permintaan sampai tanggapan yang diinginkan diterima 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);
}
});
}
Brainy Baboon