Async library inspired by CommonJS Promises/A spec
http://wiki.commonjs.org/wiki/Promises/A
Via CocoaPods:
pod 'KSDeferred'
[KSPromise promise:^(resolveType resolve, rejectType reject) {
[obj doAsyncThing:^(id value, NSError *error) {
if (error) {
reject(error);
} else {
resolve(value);
}
}];
}];
KSPromise<NSString *> *promise = [KSPromise resolve:@"A"];
KSPromise<NSString *> *promise = [KSPromise reject:[NSError errorWithDomain:@"error" code:1 userInfo:nil]];
[promise then:^id(id value) {
.. do something ..
return value;
} error:^id(NSError *e) {
.. handle error ..
return e;
}];
[promise then:^id(id value) {
.. do something ..
return value;
}];
[promise error:^id(NSError error) {
.. do something ..
return error;
}];
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;
}];
[promise finally:^ {
.. do something ..
}];
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
}];
KSPromise *waitForMe1 = ...;
KSPromise *waitForMe2 = ...;
KSPromise *joinedPromise = [KSPromise when: @[
waitForMe1, waitForMe2
]];
The method all:
is a synonym for when:
.
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;
}];
- Kurtis Seebaldt, Pivotal Labs
Copyright (c) 2013-2016 Kurtis Seebaldt. This software is licensed under the MIT License.