I am doing a validated loop with while, but inside I have a promise, I want it to repeat if it was not successful, but if it was resolved then stop repeating. the problem is that before it solves it is executing an infinite loop making the program explode, I show what I have
const validateBin = new Promise((resolve, reject) => {
while(!this.validated) {
this.findEmail() // metodo async
.then(mail => {
console.log('mail found: ', mail)
this.validated = true
resolve()
})
.catch(error => {
reject()
}
}
}
The way it can be done is by using recursion, and doing the evaluation inside the callback, not outside:
Obviously I changed the code to work here, the evaluation cannot be outside the callback, because it is asynchronous, and in this case it is not necessary to do a
if
because the evaluation is already done with the.then
or.catch
. In your code it should be something like this:I don't see the need to create another promise.