How should it be done, correctly? When the request fails, that it trys again one or two times till it stops and maybe shows a button for the user to manually repeat?
...
let failCount = 3
function getData(params) {
return new Promise((resolve, reject) => {
app.request.get('backend-url.com', params,
function(res) {
resolve(res)
},
function(err) {
failCount--
console.log(failCount)
failCount ? getData({foo:'bar'}) : showManualButtonAndResetFailCount()
reject(err)
})
})
}
console.log(app)
getData({
foo: 'bar'
})
you can also use async/await if you dont like promises, or callback()
Thanks for your reply,
This way will have to reset the failCount always before using getData
shoudln’t failCount be defined within getData like that maybe:
let failCount = failCount < 0 ? 3 : failCount;
Another Question the FailButton how could i define it that it will redo the request that didnt work or should the reset button reload the app?
I have coded it right now this way the only thing what deosn’t work is that it doesnt fire the reject function it says its undefined:
var failCount;
window.getData = function (params, resolve, reject) {
var self = this;
failCount = failCount > 0 ? failCount : 3;
if(!params.key)
params.key = '#';
if(!params.token)
params.token = localStorage.getItem('token');
app.request.json('http://example.com/api/', params,
function(res) {
resolve(res)
},function(error) {
failCount--
failCount ? getData(params) : reject(error)
})
}
Thats bcs resolve reject are for promises.