Giter Club home page Giter Club logo

promisekit's Introduction

PromiseKit aims to make dealing with assyncronicity in your iPhone app delightful. PromiseKit is not just a Promises implementation, it is also a collection of helper functions that make the typical asyncronous patterns we use in iOS development delightful too.

What is a Promise?

A promise is an intent to accomplish an asyncronous task. Eg. some library promises to download a gravatar:

#import "PromiseKit.h"

- (void)gravatar {
    [ABCDoEverythingLibrary gravatar:myURL].then(^(UIImage *img){
        self.imageView.image = img;
    }).fail(^(NSError *error){
        NSLog(@"%@", error);
    })
}

The designer changes their mind (again). They want us to overlay a kitten on the gravatar:

#import "PromiseKit.h"

- (void)gravatar {
    UIImage *gravatarImage = nil;
    
    [ABCDoEverythingLibrary gravatar:myURL].then(^(UIImage *img){
        gravatarImage = img;
    }).fail(^(NSError *error){
        NSLog(@"%@", error);
    }).then(^{
        return [NSURLConnection GET:@"http://placekitten.com/100/100"];
    }).then(^(UIImage *kittenImage){
        self.imageView.image = [UIImage draw:kittenImage over:gravatarImage];
    });
}

Whoops we didn't handle the error for the NSURLConnection:

#import "PromiseKit.h"

- (void)gravatar {
    UIImage *gravatarImage = nil;
    
    [ABCDoEverythingLibrary gravatar:myURL].then(^(UIImage *img){
        gravatarImage = img;
    }).then(^{
        return [NSURLConnection GET:@"http://placekitten.com/100/100"];
    }).then(^(UIImage *kittenImage){
        self.imageView.image = [UIImage draw:kittenImage over:gravatarImage];
    }).fail(^(NSError *error){
        // moved to the end: any errors or thrown exceptions will bubble up
        NSLog(@"%@", error);
    });
}

We need to refactor this code so our imageView is set elsewhere:

#import "PromiseKit.h"

- (Promise *)gravatar {
    UIImage *gravatarImage = nil;
    
    [ABCDoEverythingLibrary gravatar:myURL].then(^(UIImage *img){
        gravatarImage = img;
    }).then(^{
        return [NSURLConnection GET:@"http://placekitten.com/100/100"];
    }).then(^(UIImage *kittenImage){
        return [UIImage draw:kittenImage over:gravatarImage];
    });
}

- (void)viewDidLoad {
    self.gravatar.then(^(UIImage *img){
        self.imageView.image = img;
    }).fail(^(NSError *error){
        [UIAlertView show:error];
    });
}

In production we find that ABCDoEverythingLibrary is a buggy, bloated mostrosity. So we hastily reinvent the wheel:

#import "PromiseKit.h"

- (Promise *)gravatar {
    UIImage *gravatarImage = nil;
    
    [Promise md5:self.email].then(^(NSString *md5){
        // The MD5 is crunched in a background GCD queue
        return [NSURLConnection GET:@"http://gravatar.com/avatar/%@", md5];
    }).then(^{
        return [NSURLConnection GET:@"http://placekitten.com/100/100"];
    }).then(^(UIImage *kittenImage){
        return [UIImage draw:kittenImage over:gravatarImage];
    });
}

Later we realize we’re wasting our users’ lives by downloading the two images consequentively:

#import "PromiseKit.h"

- (Promise *)gravatar {
    UIImage *gravatarImage = nil;
    
    [Promise md5:self.email].then(^(NSString *md5){
        id a = [NSURLConnection GET:@"http://gravatar.com/avatar/%@", md5];
        id b = [NSURLConnection GET:@"http://placekitten.com/100/100"];
        return [Promise when:@[a, b]];
    }).then(^(NSArray *images){
        return [UIImage draw:images[0] over:images[1]];
    });
}

TODO Further Examples

  1. UIViewControllers
  2. NSURLConnections

promisekit's People

Contributors

mxcl avatar

Watchers

Reefaq Kader avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.