ES6中有一个 promise.all()
的用法, 在iOS开发中很少用到, 刚尝试了下, 在pod 'PromiseKit', '1.7.7'
中也是支持该用法的.
具体使用如下:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor orangeColor];
[PMKPromise all:@[[self task1], [self task2], [self task3], [self task4]]]
.then(^(id res){
NSLog(@"打印结果:%@", res);
})
.catch(^(NSError *error){
NSLog(@"失败结果:%@", error);
});
}
- (PMKPromise *)task1
{
return [PMKPromise new:^(PMKFulfiller fulfill, PMKRejecter reject) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"111111");
fulfill(@{@"name":@"liming", @"age":@"18"});
});
}];
}
- (PMKPromise *)task2
{
return [PMKPromise new:^(PMKFulfiller fulfill, PMKRejecter reject) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"222222");
fulfill(@[@1, @2, @3]);
});
}];
}
- (PMKPromise *)task3
{
return [PMKPromise new:^(PMKFulfiller fulfill, PMKRejecter reject) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"333333");
// NSError *error = [NSError errorWithDomain:@"" code:202 userInfo:@{@"error":@"失败了"}];
// reject(error);
fulfill(@[@"Tencent", @"Ali", @"Baidu"]);
});
}];
}
- (PMKPromise *)task4
{
return [PMKPromise new:^(PMKFulfiller fulfill, PMKRejecter reject) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"444444");
fulfill(@[@"zhangsan", @"liming", @"wangwu"]);
});
}];
}
@end
打印结果:
2020-02-15 18:32:16.979704+0800 PromiseDemo[3899:219139] 333333
2020-02-15 18:32:18.079828+0800 PromiseDemo[3899:219139] 222222
2020-02-15 18:32:18.080177+0800 PromiseDemo[3899:219139] 444444
2020-02-15 18:32:19.179937+0800 PromiseDemo[3899:219139] 111111
2020-02-15 18:32:19.180868+0800 PromiseDemo[3899:219139] 打印结果:(
{
age = 18;
name = liming;
},
(
1,
2,
3
),
(
Tencent,
Ali,
Baidu
),
(
zhangsan,
liming,
wangwu
)
)
其中 when
的使用效果和all
效果是一样的, 具体看文档解释
#import <Foundation/NSEnumerator.h>
#import <PromiseKit/Promise.h>
@interface PMKPromise (When)
/**
Returns a new promise that is fulfilled if and when all the provided promises are fulfilled.
@param promiseOrArrayOfPromisesOrValue The input upon which to wait before resolving this promise.
If an array is passed then the returned promise is fulfilled once all the provided promises are fulfilled. If *any* of the provided promises reject, the returned promise is immediately rejected with that promise’s rejection error.
@return A promise that is resolved with either:
1. An array of values from the provided array of promises.
2. The value from the provided promise.
3. The provided non-promise object.
Note that passing an `NSError` to when will reject the returned promise.
*/
+ (PMKPromise *)when:(id)promiseOrArrayOfPromisesOrValue;
/**
Alias for `+when:` provided due to ES6 specifications.
@see when
*/
+ (PMKPromise *)all:(id<NSFastEnumeration, NSObject>)enumerable;
@end
网友评论