« first day (2 days earlier)  last day (15 days later) » 

6:08 PM
@BenjaminGruenbaum I've thought some more about this. The penultimate version in your Gist does not work as intended. It always fulfills its promise. It never rejects, just stops the iteration. (See fiddle, repeat a few times to see what I mean). Whatever I do, I need to call reject() at some point, because the caller of repeatUntilSuccess() relies on it. Your design does not let me do this.
 
I... don't even remember that particular question - lemme have a quick look.
 
yeah no problem
I took my time as well, after all. ;)
 
What what the question? Iteration with condition with promises?
 
see the top of the chat ^^
...and my criterion for rejection is "the promise provider has stopped providing promises" (we can settle on "thenables", to remove the wonky instanceof Promise check), so my check is not dependent on the result of one of the promises provided earlier, it's a new condition, so I think using new Promise() is in order in this case.
 
@Tomalak something like jsfiddle.net/eead6ge7 ?
(For a while loop that is)
(If you throw then it's like you synchronously throw - an error that can be handled with a catch)
It looks much much nicer with ES6 and ES7.
 
6:25 PM
well, not quite. o_O I don't want to repeat N times. I want to repeat until success, but N times at maximum.
 
Much nicer: goo.gl/50N8pP
@Tomalak you want to do until then, not a while, that's very similar. Note that the sample is not repeating "N times", it repeats until a condition is true. That condition can be whatever you'd like.
Your condition doesn't have to be i < 5 it can also be (i < 5) && myOtherCondition()
 
Either I'm being real dumb right now or you're not talking about what I'm talking about.
 
@Tomalak well, I don't think you're being dumb I recall struggling with this myself AND there is a good chance we're not talking about the same thing.
If you explain your problem clearly and/or write sync code that does the same thing I can try to write it without the promise constructor and/or agree with you it's a valid use case for it.
 
well it's only half my problem. I'm one of the guys who answered the question linked above and you were the guy who ripped apart my answer for being un-idiomatic/containing an antipattern. so I have spent a good deal of time re-evaluating my approach and I simply can't see a better way of doing it. ;)
 
Well, I'm honoured to have gotten you to spend a good deal of time about this problem, I think that in general these are pretty interesting problems :)
Well, you can translate everything you have done in sync code to use promises - the above fiddle (and babeljs repl example) are just examples of a direct translation of a while loop. Using rejections to handle errors there isn't a great idea.
 
6:38 PM
given a function A(params) that returns a promise (that can fail or succeed) I want to write a function B(x) that repeats calling A until either A succeeds or a freely definable "abort" condition becomes true. Side complication is that params for A might have to be modifed for every call of A
Yeah, we had that discussion. ;-)
 
@Tomalak define "succeeds"
 
fulfills its promise.
in case of rejection, call A again
with modified parameters
 
Oh, that's not the same thing as a while loop - that's a "retry".
 
right.
 
Well, you can easily make that from the while above by doing a then that "break"s from the condition.
 
6:46 PM
effectively this
function B() {
    var result = A(params)
        count = 0;
    while (!result.success) {
        count++;
        if (count > 9) break;
        result = A(modify(params));
    }
    return result.success ? result : "failed after 10 tries";
}
only that I want no dependency on modify() and none on if (count > 9) (or anything like that) insde B
 
function B(){
    var count =  0;
    try{
        var result = A(params);
    } catch(e) {
       return Promise.reject(e);
    }
    if(result.success) return Promise.resolve(result);
    return Promise.resolve(result).then(function loop(result){
          count++;
          if(count > 9) return;
          return A(modify(params)).then(loop);
    }).then(function(last){
         return result.success ? result : "failed after 10 retries";
    });
}
 
in essence I also don't want a dependency on A.
 
This is just hand converted to do the same thing.
Can be nicer
 
I only want to loop when A's promise rejects.
and AFAICS this still never rejects overall
 
 
2 hours later…
8:34 PM
@Tomalak then instead of returning "failed after 10 retries' throw an exception.
 

« first day (2 days earlier)  last day (15 days later) »