I have the following code, stripped out of any specifics:
const installation = async() => {
try {
const call = await callToServer();
if('finished' in call) {
return call;
} else {
await callToServer();
}
} catch(e) {
//
}
}
And as you can see, I'd like to recursively call callToServer until I see finished inside its response. I understand why my code isn't working and why it's making only 2 calls (or 1, if it sees finished on the first run), but I don't know how to make it recursive.
I tried with a for loop and, well, it worked as-is, but in my case, I don't know the number of calls I need to make upfront. So I went to a do...while loop, since it seemed to fit:
do {
const call = await callToServer();
} while(!('finished' in call));
But this only runs once. It sees that the while(!... doesn't actually know about call. How can I make it work?
callToServer" - For recursion something has to call itself again. Nothing in your example does this. So something ininstallationwould have to callinstallationforis a generic syntax for loops. You don't need to know how many times you need to lop to usefor. For example you can dofor ( ; condition ; ) ...which will behave exactly the same aswhile (condition)for" example or some documentation on that. I had always assumed that aforloop needs to know the number of steps it needs to run for initially. However, iffor ( ; condition ; )is valid syntax, then it's just personal preference overdo...while, right? It's just weird to havefor( i=0; !finished; i++)for this use-case for me.for (;;) ..as infinite loop for a long time and it's supported in javascript as well. I've always thoughtfor (;;) ..is a stupid way to do infinite loop because#define ALWAYS 1; while (ALWAYS) ..feels more readable to me. But yeah,foris very flexible