I have a promise P which checks a condition on the server (email verified).
P can either resolve -> email verified
or fail -> with code email unverified
or fail -> with code other error (email does not exist etc)
I want to create another promise WaitP that will wait for P to either resolve or fail with a code other than email unverified.
so WaitP does:
issue P
if P resolves, resolve WaitP
if P fails with code email unverified, go back to 1 (issue P again)
if P fail with a code other than email unverified, fail WaitP
How can I write such a promise ?
I am hoping for a solution without recursion.
thx!
Just recursively call your function from the catch handler:
function waitP() {
return P().catch(function(err) {
if (err.code == "email unverified")
return waitP(); // try again
else
throw err;
});
}
You might want to add a counter or a delay to the recursive call though, so that your process doesn't hang if P() quickly and repeatedly fails ad infinitum.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With