Async library inspired by CommonJS Promises/A spec
http://wiki.commonjs.org/wiki/Promises/A
Via Cocoapods:
pod 'KSDeferred'
KSDeferred *deferred = [KSDeferred defer];
return deferred.promise;
[promise then:^id(id value) {
.. do something ..
return value;
} error:^id(NSError *e) {
.. handle error ..
return e;
}];
KSPromise *chained = [promise then:^id(id value) {
return value;
} error:^id(NSError *e) {
return e;
}];
[chained then:^id(id value) {
# value is value returned from first promise
} error:^id(NSError *e) {
# error is error returned from first promise
}];
KSPromise *chained = [promise then:^id(id value) {
KSDeferred *nextDeferred = [KSDeferred defer];
return nextDeferred.promise;
} error:^id(NSError *e) {
return e;
}];
[chained then:^id(id value) {
# value is value the returned promise resolves with
} error:^id(NSError *e) {
# error is error the returned promise rejects with
}];
KSDeferred *waitForMe1 = [KSDeferred deferred];
KSDeferred *waitForMe2 = [KSDeferred deferred];
KSPromise *joinedPromise = [KSPromise when: @[
[waitForMe1 promise],
[waitForMe2 promise]
]];
[deferred resolveWithValue:@"VALUE"];
NSError *someError;
[deferred rejectWithError:someError];
KSDeferred<NSDate *> *deferred = [KSDeferred defer];
KSPromise<NSDate *> *promise = deferred.promise;
[deferred resolveWithValue:[NSDate date]];
[promise then:^id(NSDate *date) {
.. do something ..
return date;
} error:^id(NSError *e) {
.. handle error ..
return e;
}];
- Kurtis Seebaldt, Pivotal Labs
Copyright (c) 2013 Kurtis Seebaldt. This software is licensed under the MIT License.