In my project, I need to remotely check an API and I want to do it one time, if it returns something I don't want, it has to check another time but after 5 seconds delay, then 10, then 15 then 30 seconds. If after 30 seconds still not the response I expected from API, then I throw an error to the client, but if API answers with the expected response I want (at check #1 or #2 or #3 or #4) to return something to the client and stop the checks.
this is my method:
Meteor.methods({
'verifyUserOwnership': function() {
function checkCard() {
return false; // for testing, simulating the bad answer from api
}
var checkOwnership = function(nbrTry, delay){
if (checkCard()) {
//I will do something if API responds with wwhat I want
} else {
if(nbrTry < 3){
delay +=5000;
Meteor.setTimeout(function(){checkOwnership(nbrTry+1, delay);}, delay);
} else {
throw new Meteor.Error('Card could not be found' );// has to be sent to client if after all the tries, API did respond with somwthing bad
}
}
}
checkOwnership(0, 0);
}
});
But With this code I have this error :
I20160809-12:56:06.928(-4)? Exception in setTimeout callback: Error: [Card could not be found]
I20160809-12:56:06.929(-4)? at checkOwnership (server/methods.js:74:17)
I20160809-12:56:06.929(-4)? at server/methods.js:72:40
I20160809-12:56:06.929(-4)? at [object Object]._.extend.withValue (packages/meteor/dynamics_nodejs.js:56:1)
I20160809-12:56:06.929(-4)? at packages/meteor/timers.js:6:1
I20160809-12:56:06.930(-4)? at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)
I think it's something about fiber and Meteor.bindEnvironment but I am not sure how to do it.
Thank you.