Saturday, May 11, 2024
98
rated 0 times [  99] [ 1]  / answers: 1 / hits: 19817  / 10 Years ago, sun, december 21, 2014, 12:00:00

I have a function that does several DDBB calls, so it is asynchronous.



I need to call the function and check a data value (winner) in the JSON object it returns. If it is true i need to call the function again until winner == false.



I can't use while because it is not asynchronous, how can i do this?



  someFunction(function(result) {
// if result.winner == true repeat
})

More From » asynchronous

 Answers
6

You can call the same callback function again until condition is true:



someFunction(function repeat(result) {
if (result.winner) {
someFunction(repeat);
}
});


Check the demo below.





someFunction(function repeat(result) {
document.body.innerHTML += '<br>' + result.winner;
if (result.winner) {
someFunction(repeat);
}
});

var results = [true, true, true, false];
function someFunction(callback) {
setTimeout(function() {
callback({winner: results.shift()});
}, (Math.random() + 1) * 1000 | 0);
}




[#68424] Thursday, December 18, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
annaw

Total Points: 18
Total Questions: 91
Total Answers: 98

Location: Guam
Member since Fri, Jun 18, 2021
3 Years ago
;