Code Monkey home page Code Monkey logo

rwpromisekit's Introduction

RWPromiseKit

Desiciption

A light-weighted Promise library for Objective-C

About Promise

The Promise object is used for deferred and asynchronous computations. A Promise represents an operation that hasn't completed yet, but is expected in the future. Ref

In JavaScript, Promise handles asynchronising call beatifully:

getJSON("/posts.json").then(function(posts) {
  // ...
  consume(posts);
}).catch(function(error) {
  console.log('something wrong!', error);
});

Usage of RWPromiseKit

Here is a basic example:

RWPromise* p1 = [RWPromise promise:^(ResolveHandler resolve, RejectHandler reject) {
                  resolve(@"result");
                }];
p1.then(^id(NSString* value){
  NSLog(@"%@",value); //result
  return @"resultOfThen";
}).then(^id(NSString* value){
  NSLog(@"%@",value); //resultOfThen
  NSException *e = [NSException exceptionWithName:@"name"
                                           reason:@"reason"
                                         userInfo:@{}];
  @throw e;
  return nil;
}).catch(^(NSError* error){
  NSLog(@"%@",[error description]); //error contains exception
});

Using RWPromise is exactly same as using promise in js. resolve and reject are provided in initial block as input parameters, these two methods are used to change the state of a promise. Block passed in then will be invoked when a promise is set to resolved while one in catch will be invoked when rejected.

For more infomation about the API of promise in js, please reference here

Suppoted API

0.1.0

  • then
  • catch
  • finally
  • after
  • retry
  • timeout
  • map
  • filter
  • reduce
  • race
  • all
  • resolve
  • reject

0.2.0

  • progress

Installation

  • Cocoapods
pod 'RWPromiseKit', '0.2.0'
  • Source code

Copy all source files from directory Class to your project

Issues and Todo list

  • I simplify the usage of then compared with js. Just pass only one handler block to handle when last promise is resolved. To reject, you can raise an expection or return a new promise.

  • some other API: map,filter,reduce

  • Integrate with 3rd party lib

  • Unit test are not finished

  • Complicated test cases.

  • Doc with more detail

License

Licensed under MIT. Full license here »

rwpromisekit's People

Contributors

bryant1410 avatar deput avatar

Watchers

 avatar  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.