3

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:

  1. 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!

kofifus
  • 17,260
  • 17
  • 99
  • 173
  • does [this](http://stackoverflow.com/questions/35791829/javascript-retry-promise-until-resolve/35792676#35792676) help? – georg Jun 17 '16 at 08:53
  • Have you checked [these](https://stackoverflow.com/search?q=[promise]+retry)? – Bergi Jun 17 '16 at 08:57
  • both use recursion, can it be avoided ? – kofifus Jun 17 '16 at 09:02
  • Can you justify your need for no recursion. Seems daft to not use recursion for a task where recursion suits it perfectly. – ste2425 Jun 17 '16 at 09:04
  • well the user may not verify his email for a few days for example – kofifus Jun 17 '16 at 09:11
  • @kofifus I'm afraid i still fail to see how that could cause an issue for a recursive solution? Your not wanting delays in-between each check of large periods of time. Even if you were i can't see how a recursive solution would cause any more issues. – ste2425 Jun 17 '16 at 09:17
  • if I recurse every 500ms for 2 days wouldn't that be an issue of stack overflow ? anyways, a non recursion solution is what I'm after if possible – kofifus Jun 17 '16 at 09:20
  • @kofifus: I don't think you want promises hanging around unresolved for days. There's hardly any use case justifying that. And even then, you should not resolve your promise by polling every 500ms, you should resolve it directly when the user verifies his email address. – Bergi Jun 17 '16 at 09:41
  • yes I wish I could resolve it like that but the frame work I use does not allow it. that's why I need a busy wait loop solution but not sure how to write it – kofifus Jun 17 '16 at 10:02

1 Answers1

3

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.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • 3
    @kofifus: There is absolutely nothing wrong with recursion. There is no stack in asynchronous callbacks. – Bergi Jun 17 '16 at 09:38
  • no stack ? how come can you explain ? I thought every function call leaves a trace on the stack – kofifus Jun 17 '16 at 09:47
  • @kofifus Asynchronous callbacks are created with their own stack. `waitP` is not called by `waitP` directly, but from the promise callback. – Bergi Jun 17 '16 at 09:49
  • ok but isnt then this promise callback prone to overflowing ? imagine the user waits 2 days to verify the email and P runs every 500ms – kofifus Jun 17 '16 at 10:00
  • @kofifus: [It should not](http://stackoverflow.com/q/29925948/1048572), but I still would advise against keeping promises around for 2 days anyway. – Bergi Jun 17 '16 at 10:05
  • ok Brgi the discussion there is very interesting. Do you think a non recursive solution is impossible ? – kofifus Jun 17 '16 at 10:32
  • @kofifus: I could *proof* it to you that any non-recursive approach is impossible :-) – Bergi Jun 17 '16 at 10:33
  • 1
    This isn't technically even recursion. – jib Jun 18 '16 at 16:15