Skip to content

Instantly share code, notes, and snippets.

@namanhams
Last active April 5, 2018 09:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save namanhams/54c4da489aeb658a0f3e20d63524f3cc to your computer and use it in GitHub Desktop.
Save namanhams/54c4da489aeb658a0f3e20d63524f3cc to your computer and use it in GitHub Desktop.
+ (AnyPromise *) retry:(int)count delay:(NSTimeInterval)delay block:(AnyPromise *(^)(void))block {
    AnyPromise* (^attemp)(void) = ^AnyPromise*() {
        return block().catch(^(NSError *error) {
            if(count <= 1) {
                @throw error;
            }
            return PMKAfter(delay).then(^{
                return [AnyPromise retry:count-1 delay:delay block:block];
            });
        });
    };
    
    return attemp();
}

Usage:

[AnyPromise retry:2 delay:1 block:^AnyPromise *{
        // Code that returns a AnyPromise object
        ......
 }];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment