Async library inspired by CommonJS Promises/A spec
Objective-C Objective-C++ C++ Other
Latest commit 69610f8 Jun 4, 2017 @kseebaldt committed on GitHub Merge pull request #55 from pivotaltracker/carthage-fix
Dont build Spec file when building framework for OSX
Permalink
Failed to load latest commit information.
Deferred.xcodeproj
Deferred
Specs Allow KSPromise +resolve with nil. Jun 29, 2016
.gitignore update gitignore Jun 18, 2012
.travis.yml update xcode version in travis Apr 5, 2016
KSDeferred.podspec update to 0.3.3 Jun 29, 2016
LICENSE.txt add MIT license Mar 11, 2013
README.md add error: finally: all Jun 29, 2016

README.md

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.