Giter Club home page Giter Club logo

deferred's Introduction

Async library inspired by CommonJS Promises/A spec Build Status Carthage compatible

http://wiki.commonjs.org/wiki/Promises/A

Installation

Via CocoaPods:

pod 'KSDeferred'

Examples

Creating a promise

	[KSPromise promise:^(resolveType resolve, rejectType reject) {
        [obj doAsyncThing:^(id value, NSError *error) {
			if (error) {
				reject(error);
			} else {
				resolve(value);
			}
		}];
    }];

Creating a resolved promise

    KSPromise<NSString *> *promise = [KSPromise resolve:@"A"];

Creating a rejected promise

    KSPromise<NSString *> *promise = [KSPromise reject:[NSError errorWithDomain:@"error" code:1 userInfo:nil]];

Adding callback to the promise

    [promise then:^id(id value) {
        .. do something ..
        return value;
      } error:^id(NSError *e) {
        .. handle error ..
        return e;
    }];

Shortcut methods when only one callback is needed

    [promise then:^id(id value) {
        .. do something ..
        return value;
    }];

    [promise error:^id(NSError error) {
        .. do something ..
        return error;
    }];

Chaining promises

    KSPromise *step1 = [KSPromise promise:^(resolveType resolve, rejectType reject) {
        [obj doAsyncThing:^(id value, NSError *error) {
			if (error) {
				reject(error);
			} else {
				resolve(value);
			}
		}];
    }];

    KSPromise *step2 = [step1 then:^id(id value) {
        # value is value returned from first promise
		return [obj doSomethingWith:value];
    } error:^id(NSError *e) {
        # error is error returned from first promise
		return e;
    }];

Always execute a callback, regardless of fulfillment or rejection

    [promise finally:^ {
        .. do something ..
    }];

Returning a promise from a callback to chain async work

    KSPromise *chained = [promise then:^id(id value) {
		KSPromise promise = [obj doAsyncThing];
		return promise;
    } error:^id(NSError *e) {
        return e;
    }];

    [chained then:^id(id value) {
        # value is value from doAsyncThing
    } error:^id(NSError *e) {
        # error is error from doAsyncThing
    }];

Returning a promise that completes after an array of other promises have completed

    KSPromise *waitForMe1 = ...;
    KSPromise *waitForMe2 = ...;
    
    KSPromise *joinedPromise = [KSPromise when: @[
        waitForMe1, waitForMe2
    ]];

The method all: is a synonym for when:.

Working with generics for improved type safety (Xcode 7 and higher)

    KSPromise<NSDate *> *promise = [KSPromise promise:^(resolveType resolve, rejectType reject) {
        [obj doAsyncThing:^(id value, NSError *error) {
			resolve([NSDate date]);
		}];
    }];

    [promise then:^id(NSDate *date) {
        .. do something ..
        return date;
    } error:^id(NSError *e) {
        .. handle error ..
        return e;
    }];

Author

Copyright (c) 2013-2016 Kurtis Seebaldt. This software is licensed under the MIT License.

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.