PromisesObjC

作者: 想聽丿伱說衹愛我 | 来源:发表于2020-07-03 16:08 被阅读0次

    版本:1.2.9

    Promise - 承诺
    pending - 待处理状态
    fulfilled - 已实现状态(成功情况,可进行例如更新UI操作)
    rejected - 已拒绝状态(抛出错误)

    FBLPromise.h

    @interface FBLPromise<__covariant Value> : NSObject
    
    //默认的调度队列,如果未指定队列,则为dispatch_get_main_queue()
    @property(class) dispatch_queue_t defaultDispatchQueue NS_REFINED_FOR_SWIFT;
    
    //创建一个待处理承诺。
    + (instancetype)pendingPromise NS_REFINED_FOR_SWIFT;
    
    //创建一个已解决的承诺。
    //resolution 若为NSError 则为已拒绝承诺 否则为已实现承诺
    + (instancetype)resolvedWith:(nullable id)resolution NS_REFINED_FOR_SWIFT;
    
    //同步实现承诺 只有当承诺为待处理状态才生效
    //value 若为NSError  则会拒绝该承诺
    - (void)fulfill:(nullable Value)value NS_REFINED_FOR_SWIFT;
    
    //同步拒绝承诺 只有当承诺为待处理状态才生效
    //error 拒绝承诺的错误
    - (void)reject:(NSError *)error NS_REFINED_FOR_SWIFT;
    
    @end
    
    //todo 库中并没有使用该方法 不知有何作用
    @interface FBLPromise<Value>()
    
    //将一个对象添加到一组待处理对象中,以便承诺在待处理状态时保持strongly。
    - (void)addPendingObject:(id)object NS_REFINED_FOR_SWIFT;
    
    @end
    
    //点方法 以后所有类中的点方法都全部隐藏 请自行在源文件中查看
    @interface FBLPromise<Value>(DotSyntaxAdditions)
    
    //pendingPromise的点方法
    + (instancetype (^)(void))pending FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
    //resolvedWith的点方法
    + (instancetype (^)(id __nullable))resolved FBL_PROMISES_DOT_SYNTAX NS_SWIFT_UNAVAILABLE("");
    
    @end
    
    例:
        //创建一个待处理承诺
        FBLPromise *promise1 = [FBLPromise pendingPromise];
        //创建一个已实现承诺 value = @"1"
        FBLPromise *promise2 = [FBLPromise resolvedWith:@"1"];
        NSError *error2 = [NSError errorWithDomain:@"2" code:0 userInfo:nil];
        //创建一个已拒绝承诺 error = error2
        FBLPromise *promise3 = [FBLPromise resolvedWith:error2];
        //将待处理承诺promise1变成已实现承诺 value = @"3" 该方法只对待处理承诺有效
        [promise1 fulfill:@"3"];
        //将待处理承诺promise1变成已拒绝承诺 error = error2 该方法只对待处理承诺有效
        [promise1 fulfill:error2];
        [promise1 reject:error2];
    

    FBLPromisePrivate.h

    //私有 不可调用
    @interface FBLPromise<Value>()
    
    //创建一个新的待处理承诺
    - (instancetype)initPending NS_SWIFT_UNAVAILABLE("");
    
    //创建一个已解决的承诺。
    //resolution 若为NSError 则为已拒绝承诺 否则为已实现承诺
    - (instancetype)initWithResolution:(nullable id)resolution NS_SWIFT_UNAVAILABLE("");
    
    //若原承诺为已实现承诺 则异步调用onFulfill
    //若原承诺为已拒绝承诺 则异步调用onReject
    - (void)observeOnQueue:(dispatch_queue_t)queue
                   fulfill:(FBLPromiseOnFulfillBlock)onFulfill
                    reject:(FBLPromiseOnRejectBlock)onReject NS_SWIFT_UNAVAILABLE("");
    //value 已实现承诺的值
    typedef void (^FBLPromiseOnFulfillBlock)(Value __nullable value) NS_SWIFT_UNAVAILABLE("");
    //error 已拒绝承诺的错误
    typedef void (^FBLPromiseOnRejectBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //若原承诺为已实现承诺 则异步调用chainedFulfill
    //若原承诺为已拒绝承诺 则异步调用chainedReject
    //根据chainedFulfill或chainedReject的返回值类型将待处理承诺状态改变
    //返回值类型为NSError 变成已拒绝 FBLPromise 变成同状态 其他 变成已实现
    - (FBLPromise *)chainOnQueue:(dispatch_queue_t)queue
                  chainedFulfill:(FBLPromiseChainedFulfillBlock)chainedFulfill
                   chainedReject:(FBLPromiseChainedRejectBlock)chainedReject NS_SWIFT_UNAVAILABLE("");
    
    @end
    

    FBLPromise+Await.h

    //新开一个队列,并上该队列上异步等待承诺解决
    //error 传入错误的地址,若承诺已拒绝,返回该错误,如果承诺已实现,则为nil
    //返回该承诺的值 若承诺已拒绝 返回nil
    FOUNDATION_EXTERN id __nullable FBLPromiseAwait(FBLPromise *promise,
                                                    NSError **error) NS_REFINED_FOR_SWIFT;
    
    例:
        __block NSError *error;
        FBLPromise *promise4 = [FBLPromise pendingPromise];
        NSLog(@"%@", promise4);
        //需要新开线程 不然会堵塞主线程
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSString *value = FBLPromiseAwait(promise4, &error);
            NSLog(@"%@", promise4);
            NSLog(@"%@ %@", promise4.value, value);
            NSLog(@"\n%@\n%@", promise4.error, error);
        });
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //将promise4变成已实现承诺或已拒绝承诺后 FBLPromiseAwait就不会在堵塞线程
            [promise4 fulfill:@"promise4"];
    //        [promise4 fulfill:[NSError errorWithDomain:@"promise4_error" code:0 userInfo:nil]];
        });
    输出:
    <FBLPromise 0x60000324c600> Pending
    <FBLPromise 0x60000324c600> Fulfilled: promise4
    promise4 promise4
    (null)
    (null)
    

    FBLPromise+Testing.h

    //等待所有预定的承诺。
    //timeout 等待的最长时间。
    //如果所有的承诺在时限之内完成,则返回YES,否则为NO。
    FOUNDATION_EXTERN BOOL FBLWaitForPromisesWithTimeout(NSTimeInterval timeout) NS_REFINED_FOR_SWIFT;
    
    @interface FBLPromise<Value>(TestingAdditions)
    
    //承诺的调度组 只读
    @property(class, nonatomic, readonly) dispatch_group_t dispatchGroup NS_REFINED_FOR_SWIFT;
    
    //承诺的当前状态 只读
    //待处理
    @property(nonatomic, readonly) BOOL isPending NS_REFINED_FOR_SWIFT;
    //已实现
    @property(nonatomic, readonly) BOOL isFulfilled NS_REFINED_FOR_SWIFT;
    //已拒绝
    @property(nonatomic, readonly) BOOL isRejected NS_REFINED_FOR_SWIFT;
    
    //承诺已实现后的值 只读
    //如果承诺仍为待处理或已拒绝时,则为nil
    @property(nonatomic, readonly, nullable) Value value NS_REFINED_FOR_SWIFT;
    
    //承诺已拒绝后的错误 只读
    //如果承诺仍为待处理或已实现时,则为nil。
    @property(nonatomic, readonly, nullable) NSError *error NS_REFINED_FOR_SWIFT;
    
    @end
    

    FBLPromise+Do.h

    @interface FBLPromise<Value>(DoAdditions)
    
    //创建一个待处理承诺并异步执行work
    //根据work返回的类型将待处理承诺的状态改变
    //NSError时会将状态变成已拒绝 FBLPromise时会将状态变成FBLPromise的状态 其他类型时会状态变成已实现
    + (instancetype)do:(FBLPromiseDoWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    typedef id __nullable (^FBLPromiseDoWorkBlock)(void) NS_SWIFT_UNAVAILABLE("");
    
    + (instancetype)onQueue:(dispatch_queue_t)queue do:(FBLPromiseDoWorkBlock)work NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
        FBLPromise *promise2 = [FBLPromise resolvedWith:@"1"];
        NSError *error2 = [NSError errorWithDomain:@"2" code:0 userInfo:nil];
        //创建一个已拒绝承诺 error = error2
        FBLPromise *promise3 = [FBLPromise resolvedWith:error2];
        FBLPromise *promise5 = [FBLPromise do:^id _Nullable{
            //创建一个value为promise5的已实现承诺
            return @"promise5";
            //创建一个error为error2的已拒绝承诺
    //        return error2;
            //创建一个value为promise2.vlaue的已实现承诺
    //        return promise2;
            //创建一个error为promise3.error的已拒绝承诺
    //        return promise3;
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise5);
        });
    输出:
    <FBLPromise 0x600000c283f0> Fulfilled: promise5
    

    FBLPromise+Async.h

    @interface FBLPromise<Value>(AsyncAdditions)
    
    //创建一个待处理承诺并异步执行work
    + (instancetype)async:(FBLPromiseAsyncWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    //fulfill 实现承诺的回调
    //reject 拒绝承诺的回调
    typedef void (^FBLPromiseAsyncWorkBlock)(FBLPromiseFulfillBlock fulfill,
                                             FBLPromiseRejectBlock reject) NS_SWIFT_UNAVAILABLE("");
    //调用该回调时
    //若value为NSError时会将状态变成已拒绝 FBLPromise时会将状态变成FBLPromise的状态 其他类型时会状态变成已实现
    typedef void (^FBLPromiseFulfillBlock)(Value __nullable value) NS_SWIFT_UNAVAILABLE("");
    //调用该回调时会将待处理承诺状态变成已拒绝
    //error 为已拒绝承诺的错误
    typedef void (^FBLPromiseRejectBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
    
    + (instancetype)onQueue:(dispatch_queue_t)queue
                      async:(FBLPromiseAsyncWorkBlock)work NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
        FBLPromise *promise2 = [FBLPromise resolvedWith:@"1"];
        FBLPromise *promise6 = [FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
            //创建value为promise6的已实现承诺
            fulfill(@"promise6");
            //创建一个error为error2的已拒绝承诺
    //        fulfill(error2);
    //        reject(error2);
            //创建一个与promise2同状态的承诺
    //        fulfill(promise2);
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise6);
        });
    输出:
    <FBLPromise 0x6000039bcae0> Fulfilled: promise6
    

    FBLPromise+All.h

    @interface FBLPromise<Value>(AllAdditions)
    
    //创建一个待处理承诺
    //若promises为空时 返回value=@[]的已实现承诺
    //若promises元素类型可为NSError FBLPromise 其他类型
    //全部都是已实现承诺时会异步将待处理承诺状态变成已实现 该承诺的value为所有的承诺的value的数组
    //有已拒绝承诺时会异步将待处理承诺状态变为已拒绝,且error为第一个已拒绝承诺的error
    + (FBLPromise<NSArray *> *)all:(NSArray *)promises NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue
                                   all:(NSArray *)promises NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
        FBLPromise *promise7_1 = [FBLPromise pendingPromise];
        FBLPromise *promise7_2 = [FBLPromise pendingPromise];
        FBLPromise *promise7 = [FBLPromise all:@[promise7_1, promise7_2]];
        [promise7_1 fulfill:@"promise7_1"];
        [promise7_2 fulfill:@"promise7_2"];
    //    [promise7_1 fulfill:[NSError errorWithDomain:@"promise7_1_error" code:0 userInfo:nil]];
    //    [promise7_2 fulfill:[NSError errorWithDomain:@"promise7_2_error" code:0 userInfo:nil]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise7);
        });
    输出:
    <FBLPromise 0x60000243e2e0> Fulfilled: (
        "promise7_1",
        "promise7_2"
    )
    若为
    //    [promise7_1 fulfill:@"promise7_1"];
        [promise7_2 fulfill:@"promise7_2"];
        [promise7_1 fulfill:[NSError errorWithDomain:@"promise7_1_error" code:0 userInfo:nil]];
    //    [promise7_2 fulfill:[NSError errorWithDomain:@"promise7_2_error" code:0 userInfo:nil]];
    输出
    <FBLPromise 0x600000eac180> Rejected: Error Domain=promise7_1_error Code=0 "(null)"
    

    FBLPromise+Any.h

    @interface FBLPromise<Value>(AnyAdditions)
    
    //创建一个待处理承诺
    //若promises为空时 返回value=@[]的已实现承诺
    //若promises元素类型可为NSError FBLPromise 其他类型
    //如果所有承诺状态都是已拒绝,会异步将待处理承诺状态变为已拒绝,且error为第一个已拒绝承诺的error
    //如果至少有一个承诺状态为已实现,会异步将待处理承诺状态变为已实现,且该承诺的value为所有的承诺的value和NSError的数组
    + (FBLPromise<NSArray *> *)any:(NSArray *)promises NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSArray *> *)onQueue:(dispatch_queue_t)queue
                                   any:(NSArray *)promises NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
        FBLPromise *promise7_1 = [FBLPromise pendingPromise];
        FBLPromise *promise7_2 = [FBLPromise pendingPromise];
        FBLPromise *promise7 = [FBLPromise all:@[promise7_1, promise7_2]];
        [promise7_1 fulfill:@"promise7_1"];
    //    [promise7_2 fulfill:@"promise7_2"];
    //    [promise7_1 fulfill:[NSError errorWithDomain:@"promise7_1_error" code:0 userInfo:nil]];
        [promise7_2 fulfill:[NSError errorWithDomain:@"promise7_2_error" code:0 userInfo:nil]];
    
        FBLPromise *promise8 = [FBLPromise any:@[promise7_1, promise7_2]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise8);
        });
    输出:
    <FBLPromise 0x600002968b40> Fulfilled: (
        "promise7_1",
        "Error Domain=promise7_2_error Code=0 \"(null)\""
    )
    若为
    //    [promise7_1 fulfill:@"promise7_1"];
    //    [promise7_2 fulfill:@"promise7_2"];
        [promise7_1 fulfill:[NSError errorWithDomain:@"promise7_1_error" code:0 userInfo:nil]];
        [promise7_2 fulfill:[NSError errorWithDomain:@"promise7_2_error" code:0 userInfo:nil]];
    输出:
    <FBLPromise 0x6000017c0c00> Rejected: Error Domain=promise7_1_error Code=0 "(null)"
    

    FBLPromise+Race.h

    @interface FBLPromise<Value>(RaceAdditions)
    
    //创建一个待处理承诺
    //promises不能为空 
    //若数组元素类型只有FBLPromise 则异步将待处理承诺状态变成第一个承诺(非待处理)的状态 
    //若数组元素类型有FBLPromise或NSError或其他类型 排除FBLPromise后异步将待处理承诺状态变成已拒绝或已实现
    + (instancetype)race:(NSArray *)promises NS_SWIFT_UNAVAILABLE("");
    
    + (instancetype)onQueue:(dispatch_queue_t)queue race:(NSArray *)promises NS_REFINED_FOR_SWIFT;
    
    @end
    
    例:
        FBLPromise *promise7_1 = [FBLPromise pendingPromise];
        FBLPromise *promise7_2 = [FBLPromise pendingPromise];
        FBLPromise *promise7 = [FBLPromise all:@[promise7_1, promise7_2]];
        [promise7_1 fulfill:@"promise7_1"];
    //    [promise7_2 fulfill:@"promise7_2"];
    //    [promise7_1 fulfill:[NSError errorWithDomain:@"promise7_1_error" code:0 userInfo:nil]];
    //    [promise7_2 fulfill:[NSError errorWithDomain:@"promise7_2_error" code:0 userInfo:nil]];
    
        FBLPromise *promise9 = [FBLPromise race:@[promise7_1, promise7_2]];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise9);
        });
    输出:为最先从待处理改变状态的承诺
    <FBLPromise 0x600000aa95c0> Fulfilled: promise7_1
    若为
    //    [promise7_1 fulfill:@"promise7_1"];
    //    [promise7_2 fulfill:@"promise7_2"];
    //    [promise7_1 fulfill:[NSError errorWithDomain:@"promise7_1_error" code:0 userInfo:nil]];
        [promise7_2 fulfill:[NSError errorWithDomain:@"promise7_2_error" code:0 userInfo:nil]];
    输出:
    <FBLPromise 0x6000018b7cc0> Rejected: Error Domain=promise7_2_error Code=0 "(null)"
    

    FBLPromise+Retry.h

    @interface FBLPromise<Value>(RetryAdditions)
    
    //FBLPromiseRetryDefaultAttemptsCount 默认重试次数为1
    //FBLPromiseRetryDefaultDelayInterval 默认重试延迟的间隔为1.0秒
    
    //创建一个待处理承诺并异步执行work
    //work返回类型可为FBLPromise、NSError 和其他类型
    //根据返回类型异步将待处理承诺改变状态
    //若改变后的状态为已实现 则将待处理承诺状态变成已实现
    //若改变后的状态为已拒绝 则最多重试FBLPromiseRetryDefaultAttemptsCount次
    //并间隔FBLPromiseRetryDefaultDelayInterval秒重试work。
    //重试次数为0后 则异步将待处理承诺状态变成已拒绝
    + (FBLPromise *)retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    typedef id __nullable (^FBLPromiseRetryWorkBlock)(void) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise *)onQueue:(dispatch_queue_t)queue
                      retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    
    //count 最大重试次数。如果指定的计数小于或等于零,则将默认为1
    + (FBLPromise *)attempts:(NSInteger)count
                       retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise *)onQueue:(dispatch_queue_t)queue
                   attempts:(NSInteger)count
                      retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    
    //interval 重试的间隔时间
    //predicate 重试要检查的条件。
    //若predicate返回YES则会在重试次数为0之前一直重试
    //若predicate返回NO则会直接将待处理承诺状态变为已拒绝
    + (FBLPromise *)attempts:(NSInteger)count
                       delay:(NSTimeInterval)interval
                   condition:(nullable FBLPromiseRetryPredicateBlock)predicate
                       retry:(FBLPromiseRetryWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    //NSInteger 剩余重试次数
    //NSError 之前重试的已拒绝承诺的错误
    typedef BOOL (^FBLPromiseRetryPredicateBlock)(NSInteger, NSError *) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise *)onQueue:(dispatch_queue_t)queue
                   attempts:(NSInteger)count
                      delay:(NSTimeInterval)interval
                  condition:(nullable FBLPromiseRetryPredicateBlock)predicate
                      retry:(FBLPromiseRetryWorkBlock)work NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
            FBLPromise *promise10_1 = [FBLPromise pendingPromise];
            [promise10_1 fulfill:@"promise10_1"];
    //        [promise10_1 fulfill:[NSError errorWithDomain:@"promise10_1_error" code:0 userInfo:nil]];
            FBLPromise *promise10 = [FBLPromise attempts:5 delay:0.2 condition:^BOOL(NSInteger count, NSError * _Nonnull error) {
                NSLog(@"%ld %@", count, error);
                return YES;
    //            return NO;
            } retry:^id _Nullable{
                return promise10_1;
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", promise10);
            });
    输出:
    <FBLPromise 0x600000a6d290> Fulfilled: promise10_1
    若为
            FBLPromise *promise10_1 = [FBLPromise pendingPromise];
    //        [promise10_1 fulfill:@"promise10_1"];
            [promise10_1 fulfill:[NSError errorWithDomain:@"promise10_1_error" code:0 userInfo:nil]];
            FBLPromise *promise10 = [FBLPromise attempts:5 delay:0.2 condition:^BOOL(NSInteger count, NSError * _Nonnull error) {
                NSLog(@"%ld %@", count, error);
                return YES;
    //            return NO;
            } retry:^id _Nullable{
                return promise10_1;
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", promise10);
            });
    输出:
    5 Error Domain=promise10_1_error Code=0 "(null)"
    4 Error Domain=promise10_1_error Code=0 "(null)"
    3 Error Domain=promise10_1_error Code=0 "(null)"
    2 Error Domain=promise10_1_error Code=0 "(null)"
    1 Error Domain=promise10_1_error Code=0 "(null)"
    <FBLPromise 0x600002468e10> Rejected: Error Domain=promise10_1_error Code=0 "(null)"
    若为
            FBLPromise *promise10_1 = [FBLPromise pendingPromise];
    //        [promise10_1 fulfill:@"promise10_1"];
            [promise10_1 fulfill:[NSError errorWithDomain:@"promise10_1_error" code:0 userInfo:nil]];
            FBLPromise *promise10 = [FBLPromise attempts:5 delay:0.2 condition:^BOOL(NSInteger count, NSError * _Nonnull error) {
                NSLog(@"%ld %@", count, error);
    //            return YES;
                return NO;
            } retry:^id _Nullable{
                return promise10_1;
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", promise10);
            });
    输出:
    5 Error Domain=promise10_1_error Code=0 "(null)"
    <FBLPromise 0x600001c59a70> Rejected: Error Domain=promise10_1_error Code=0 "(null)"
    

    FBLPromise+Wrap.h

    //提供一种简单的方法来将回调转换为promise。
    @interface FBLPromise<Value>(WrapAdditions)
    
    //创建一个待处理承诺
    //调用handler()后异步将待处理承诺变成value为nil的已实现承诺
    + (instancetype)wrapCompletion:(void (^)(FBLPromiseCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseCompletion)(void) NS_SWIFT_UNAVAILABLE("");
    
    + (instancetype)onQueue:(dispatch_queue_t)queue
             wrapCompletion:(void (^)(FBLPromiseCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(value)后根据value类型异步将待处理承诺变成其他状态
    //value类型可为FBLPromise、NSError 和其他类型
    //NSError 变成已拒绝 FBLPromise 变成同状态 其他 变成已实现
    + (instancetype)wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseObjectCompletion)(id __nullable) NS_SWIFT_UNAVAILABLE("");
    
    + (instancetype)onQueue:(dispatch_queue_t)queue
        wrapObjectCompletion:(void (^)(FBLPromiseObjectCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(error)后异步将待处理承诺变成error为error的已拒绝承诺
    //若error为空则异步将待处理承诺变成value为nil的已拒绝承诺
    + (instancetype)wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseErrorCompletion)(NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
    
    + (instancetype)onQueue:(dispatch_queue_t)queue
        wrapErrorCompletion:(void (^)(FBLPromiseErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(value, error)后异步改完待处理承诺的状态
    //若error不为nil则将状态变成已拒绝
    //否则根据value的类型将状态变成已完成或已拒绝
    + (instancetype)wrapObjectOrErrorCompletion:
        (void (^)(FBLPromiseObjectOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseObjectOrErrorCompletion)(id __nullable, NSError* __nullable)
    
    + (instancetype)onQueue:(dispatch_queue_t)queue
        wrapObjectOrErrorCompletion:(void (^)(FBLPromiseObjectOrErrorCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //与wrapObjectOrErrorCompletion方法效果一致
    + (instancetype)wrapErrorOrObjectCompletion:
        (void (^)(FBLPromiseErrorOrObjectCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseErrorOrObjectCompletion)(NSError* __nullable, id __nullable)  NS_SWIFT_UNAVAILABLE("");
    
    + (instancetype)onQueue:(dispatch_queue_t)queue
        wrapErrorOrObjectCompletion:(void (^)(FBLPromiseErrorOrObjectCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(value1, value2, error)后异步改变待处理承诺的状态
    //若error不为nil则将状态变成已拒绝
    //否则将状态变成已实现且value=@[value1, value2]
    + (FBLPromise<NSArray*>*)wrap2ObjectsOrErrorCompletion:
        (void (^)(FBLPromise2ObjectsOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromise2ObjectsOrErrorCompletion)(id __nullable, id __nullable,
                                                        NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSArray*>*)onQueue:(dispatch_queue_t)queue
        wrap2ObjectsOrErrorCompletion:(void (^)(FBLPromise2ObjectsOrErrorCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(bool)后异步将待处理承诺状态变成已实现且vlaue为@(bool)
    + (FBLPromise<NSNumber*>*)wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseBoolCompletion)(BOOL) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
                   wrapBoolCompletion:(void (^)(FBLPromiseBoolCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(bool, error)后异步将待处理承诺状态改变
    //若error不为空 状态变成已拒绝
    //否则状态变成已实现且vlaue为@(bool)
    + (FBLPromise<NSNumber*>*)wrapBoolOrErrorCompletion:
        (void (^)(FBLPromiseBoolOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseBoolOrErrorCompletion)(BOOL, NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
            wrapBoolOrErrorCompletion:(void (^)(FBLPromiseBoolOrErrorCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(integer)后异步将待处理承诺状态变成已实现且vlaue为@(integer)
    + (FBLPromise<NSNumber*>*)wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseIntegerCompletion)(NSInteger) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
                wrapIntegerCompletion:(void (^)(FBLPromiseIntegerCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(integer, error)后异步将待处理承诺状态改变
    //若error不为空 状态变成已拒绝
    //否则状态变成已实现且vlaue为@(integer)
    + (FBLPromise<NSNumber*>*)wrapIntegerOrErrorCompletion:
        (void (^)(FBLPromiseIntegerOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseIntegerOrErrorCompletion)(NSInteger, NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
         wrapIntegerOrErrorCompletion:(void (^)(FBLPromiseIntegerOrErrorCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(double)后异步将待处理承诺状态变成已实现且vlaue为@(double)
    + (FBLPromise<NSNumber*>*)wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseDoubleCompletion)(double) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
                 wrapDoubleCompletion:(void (^)(FBLPromiseDoubleCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    //创建一个待处理承诺
    //调用handler(double, error)后异步将待处理承诺状态改变
    //若error不为空 状态变成已拒绝
    //否则状态变成已实现且vlaue为@(double)
    + (FBLPromise<NSNumber*>*)wrapDoubleOrErrorCompletion:
        (void (^)(FBLPromiseDoubleOrErrorCompletion handler))work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseDoubleOrErrorCompletion)(double, NSError* __nullable) NS_SWIFT_UNAVAILABLE("");
    
    + (FBLPromise<NSNumber*>*)onQueue:(dispatch_queue_t)queue
          wrapDoubleOrErrorCompletion:(void (^)(FBLPromiseDoubleOrErrorCompletion handler))work
        NS_SWIFT_UNAVAILABLE("");
    
    @end
    
    例:
        FBLPromise *promise11 = [FBLPromise wrapCompletion:^(FBLPromiseCompletion  _Nonnull handler) {
            //创建value=nil的已实现承诺
            handler();
        }];
        promise11 = [FBLPromise wrapObjectCompletion:^(FBLPromiseObjectCompletion  _Nonnull handler) {
            //创建value=promise11的已实现承诺
            handler(@"promise11");
            //创建error=error11的已拒绝承诺
    //        NSError *error11 = [NSError errorWithDomain:@"promise11_error" code:0 userInfo:nil];
    //        handler(error11);
        }];
        promise11 = [FBLPromise wrapObjectOrErrorCompletion:^(FBLPromiseObjectOrErrorCompletion  _Nonnull handler) {
            NSError *error11 = [NSError errorWithDomain:@"promise11_error" code:0 userInfo:nil];
            //创建error=error11的已拒绝承诺
            handler(@"promise11", error11);
    //        handler(error11, nil);
            //创建value=promise11的已实现承诺
    //        handler(@"promise11", nil);
        }];
        promise11 = [FBLPromise wrap2ObjectsOrErrorCompletion:^(FBLPromise2ObjectsOrErrorCompletion  _Nonnull handler) {
            NSError *error11 = [NSError errorWithDomain:@"promise11_error" code:0 userInfo:nil];
            //创建error=error11的已拒绝承诺
            handler(@"promise11", @"promise11_", error11);
            //创建value=@[@"promise11", @"promise11_"]的已实现承诺
    //        handler(@"promise11", @"promise11_", nil);
        }];
        promise11 = [FBLPromise wrapBoolCompletion:^(FBLPromiseBoolCompletion  _Nonnull handler) {
            //创建value=@(YES)的已实现承诺
            handler(YES);
        }];
        promise11 = [FBLPromise wrapBoolOrErrorCompletion:^(FBLPromiseBoolOrErrorCompletion  _Nonnull handler) {
            NSError *error11 = [NSError errorWithDomain:@"promise11_error" code:0 userInfo:nil];
            //创建error=error11的已拒绝承诺
            handler(NO, error11);
            //创建value=@(NO)的已实现承诺
    //        handler(NO, nil);
        }];
    

    对已实现承诺生效

    FBLPromise+Then.h

    @interface FBLPromise<Value>(ThenAdditions)
    
    //创建一个待处理承诺
    //若原承诺是已拒绝承诺 异步将待处理承诺的状态变成已拒绝 不会调用work
    //若原承诺是已实现承诺 则会调用work 根据work的返回类型来异步改变状态
    //work的返回类型 NSError 变成已拒绝  FBLPromise 变成同状态 其他 变成已实现
    - (FBLPromise *)then:(FBLPromiseThenWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    //若原承诺是已实现承诺 则value为原承诺的value 若为已拒绝承诺 不会调用work
    typedef id __nullable (^FBLPromiseThenWorkBlock)(Value __nullable value) NS_SWIFT_UNAVAILABLE("");
    
    - (FBLPromise *)onQueue:(dispatch_queue_t)queue
                       then:(FBLPromiseThenWorkBlock)work NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
        FBLPromise *promise12 = [FBLPromise pendingPromise];
        FBLPromise *promise12_1 = [promise12 then:^id _Nullable(id  _Nullable value) {
            NSLog(@"value:%@", value);
            return @"promise12_1";
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [promise12 fulfill:[NSError errorWithDomain:@"promise12_error" code:0 userInfo:nil]];
    //        [promise12 fulfill:@"promise12"];
        });
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise12_1);
        });
    输出:
    <FBLPromise 0x600003d8e250> Rejected: Error Domain=promise12_error Code=0 "(null)"
    若为
    //        [promise12 fulfill:[NSError errorWithDomain:@"promise12_error" code:0 userInfo:nil]];
            [promise12 fulfill:@"promise12"];
    输出:
    value:promise12
    <FBLPromise 0x6000034f97d0> Fulfilled: promise12_1
    

    FBLPromise+Delay.h

    @interface FBLPromise<Value>(DelayAdditions)
    
    //创建一个待处理承诺
    //若原承诺为已拒绝承诺 异步将待处理承诺状态变成已拒绝
    //若原承诺为已实现承诺 异步并延时interval秒后将待处理承诺状态变成已实现
    - (FBLPromise *)delay:(NSTimeInterval)interval NS_SWIFT_UNAVAILABLE("");
    
    - (FBLPromise *)onQueue:(dispatch_queue_t)queue
                      delay:(NSTimeInterval)interval NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
        FBLPromise *promise13 = [FBLPromise pendingPromise];
        [promise13 fulfill:@"promise13"];
    //    [promise13 fulfill:[NSError errorWithDomain:@"promise13_error" code:0 userInfo:nil]];
        FBLPromise *promise13_1 = [promise13 delay:2];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.6 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise13_1);
        });
    输出:
    <FBLPromise 0x60000065f9f0> Fulfilled: promise13
    若为
    //    [promise13 fulfill:@"promise13"];
        [promise13 fulfill:[NSError errorWithDomain:@"promise13_error" code:0 userInfo:nil]];
    输出:
    <FBLPromise 0x60000181ee20> Rejected: Error Domain=promise13_error Code=0 "(null)"
    

    FBLPromise+Reduce

    @interface FBLPromise<Value>(ReduceAdditions)
    
    //先返回原承诺
    //若原承诺为已拒绝的承诺 不会调用reducer 并异步原承诺状态变成已拒绝
    //若原承诺为已实现的承诺 并异步调用reducer 并根据reducer的返回值类型将原承诺状态改变(按最后一个item获取value) 
    //返回值类型为NSError 变成已拒绝 FBLPromise 变成同状态 其他 变成已实现
    //若reduce返回NSError后导致原承诺变成为已拒绝承诺后 不会再调用reducer
    - (FBLPromise *)reduce:(NSArray *)items
                   combine:(FBLPromiseReducerBlock)reducer NS_SWIFT_UNAVAILABLE("");
    //partial 原承诺的value
    //next items的元素
    typedef id __nullable (^FBLPromiseReducerBlock)(Value __nullable partial, id next)
    
    - (FBLPromise *)onQueue:(dispatch_queue_t)queue
                     reduce:(NSArray *)items
                    combine:(FBLPromiseReducerBlock)reducer NS_SWIFT_UNAVAILABLE("");
    
    @end
    
    
    例:
            FBLPromise *promise14 = [FBLPromise pendingPromise];
            [promise14 fulfill:@"promise14"];
            //    [promise14 fulfill:[NSError errorWithDomain:@"promise14_error" code:0 userInfo:nil]];
            FBLPromise *promise14_1 = [promise14 reduce:@[@"1", @"2", @"3"] combine:^id _Nullable(id  _Nullable partial, id  _Nonnull next) {
                NSLog(@"partial:%@ next:%@", partial, next);
                return next;
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", promise14_1);
            });
    输出:
    partial:promise14 next:1
    partial:1 next:2
    partial:2 next:3
    <FBLPromise 0x600001da4b70> Fulfilled: 3
    若为
            FBLPromise *promise14 = [FBLPromise pendingPromise];
            [promise14 fulfill:@"promise14"];
            //    [promise14 fulfill:[NSError errorWithDomain:@"promise14_error" code:0 userInfo:nil]];
            FBLPromise *promise14_1 = [promise14 reduce:@[@"1", [NSError errorWithDomain:@"promise14_1_erro" code:0 userInfo:nil], @"3"] combine:^id _Nullable(id  _Nullable partial, id  _Nonnull next) {
                NSLog(@"partial:%@ next:%@", partial, next);
                return next;
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", promise14_1);
            });
    输出:
    partial:promise14 next:1
    partial:1 next:Error Domain=promise14_1_erro Code=0 "(null)"
    <FBLPromise 0x60000102c2a0> Rejected: Error Domain=promise14_1_erro Code=0 "(null)"
    

    FBLPromise+Validate.h

    @interface FBLPromise<Value>(ValidateAdditions)
    
    //创建一个待处理承诺
    //若原承诺状态为已拒绝 不调用predicate 异步将待处理的承诺的状态变为已拒绝
    //若原承诺状态为已实现 异步调用predicate并将待处理的承诺的状态改变
    //若predicate返回YES 将待处理的承诺状态变成已实现 若返回NO 将待处理的承诺状态变成已拒绝 
    //已拒绝时错误为[[NSError alloc] initWithDomain:FBLPromiseErrorDomain code:FBLPromiseErrorCodeValidationFailure userInfo:nil]
    - (FBLPromise *)validate:(FBLPromiseValidateWorkBlock)predicate NS_SWIFT_UNAVAILABLE("");
    //value 原承诺的value
    typedef BOOL (^FBLPromiseValidateWorkBlock)(Value __nullable value) NS_SWIFT_UNAVAILABLE("");
    
    - (FBLPromise *)onQueue:(dispatch_queue_t)queue
                   validate:(FBLPromiseValidateWorkBlock)predicate NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
        FBLPromise *promise15 = [FBLPromise pendingPromise];
        [promise15 fulfill:@"promise15"];
    //    [promise15 fulfill:[NSError errorWithDomain:@"promise15_error" code:0 userInfo:nil]];
        FBLPromise *promise15_1 = [promise15 validate:^BOOL(id  _Nullable value) {
            return YES;
    //        return NO;
        }];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise15_1);
        });
    输出:
    <FBLPromise 0x6000035dff00> Fulfilled: promise15
    若为
    //        return YES;
            return NO;
    输出:
    <FBLPromise 0x600000cdbb70> Rejected: Error Domain=com.google.FBLPromises.Error Code=2 "(null)"
    

    对已实现或已拒绝承诺生效

    FBLPromise+Always.h

    @interface FBLPromise<Value>(AlwaysAdditions)
    
    //创建一个待处理承诺
    //不论原承诺是已实现或已拒绝承诺 都会调用work
    //异步将待处理承诺状态变成与原承诺相同
    - (FBLPromise *)always:(FBLPromiseAlwaysWorkBlock)work NS_SWIFT_UNAVAILABLE("");
    typedef void (^FBLPromiseAlwaysWorkBlock)(void) NS_SWIFT_UNAVAILABLE("");
    
    - (FBLPromise *)onQueue:(dispatch_queue_t)queue
                     always:(FBLPromiseAlwaysWorkBlock)work NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
            FBLPromise *promise16 = [FBLPromise pendingPromise];
            [promise16 fulfill:@"promise16"];
    //        [promise16 fulfill:[NSError errorWithDomain:@"promise16_error" code:0 userInfo:nil]];
            [promise16 always:^{
                NSLog(@"%@", promise16);
            }];
    输出:
    <FBLPromise 0x600001d04cc0> Fulfilled: promise16
    若为
    //        [promise16 fulfill:@"promise16"];
            [promise16 fulfill:[NSError errorWithDomain:@"promise16_error" code:0 userInfo:nil]];
    输出:
    <FBLPromise 0x600000bd40c0> Rejected: Error Domain=promise16_error Code=0 "(null)"
    

    FBLPromise+Timeout.h

    @interface FBLPromise<Value>(TimeoutAdditions)
    
    //创建一个待处理承诺
    //异步将待处理承诺变成与原承诺同状态
    //若interval秒之后待处理承诺状态没有变化 则将待处理承诺状态变成已拒绝
    //此时error = [[NSError alloc] initWithDomain:FBLPromiseErrorDomain code:FBLPromiseErrorCodeTimedOut userInfo:nil]
    - (FBLPromise *)timeout:(NSTimeInterval)interval NS_SWIFT_UNAVAILABLE("");
    
    - (FBLPromise *)onQueue:(dispatch_queue_t)queue
                    timeout:(NSTimeInterval)interval NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
        FBLPromise *promise17 = [FBLPromise pendingPromise];
        [promise17 fulfill:@"promise17"];
    //    [promise17 fulfill:[NSError errorWithDomain:@"promise17_error" code:0 userInfo:nil]];
        FBLPromise *promise17_1 = [promise17 timeout:2];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            NSLog(@"%@", promise17_1);
        });
    输出:
    <FBLPromise 0x600002d2f3f0> Fulfilled: promise17
    若为
    //    [promise17 fulfill:@"promise17"];
    //    [promise17 fulfill:[NSError errorWithDomain:@"promise17_error" code:0 userInfo:nil]];
    输出:
    <FBLPromise 0x600000a65800> Rejected: Error Domain=com.google.FBLPromises.Error Code=1 "(null)"
    

    对已拒绝承诺生效

    FBLPromise+Catch.h

    @interface FBLPromise<Value>(CatchAdditions)
    
    //创建一个待处理承诺
    //若原承诺为已拒绝承诺 会异步调用reject并将待处理承诺状态变成已拒绝
    //若原承诺为已实现承诺 不会调用reject 异步将待处理承诺状态变成已实现
    - (FBLPromise *)catch:(FBLPromiseCatchWorkBlock)reject NS_SWIFT_UNAVAILABLE("");
    //error 已拒绝承诺的错误 
    typedef void (^FBLPromiseCatchWorkBlock)(NSError *error) NS_SWIFT_UNAVAILABLE("");
    
    - (FBLPromise *)onQueue:(dispatch_queue_t)queue
                      catch:(FBLPromiseCatchWorkBlock)reject NS_REFINED_FOR_SWIFT;
    
    @end
    
    例:
            FBLPromise *promise18 = [FBLPromise pendingPromise];
    //        [promise18 fulfill:@"promise18"];
            [promise18 fulfill:[NSError errorWithDomain:@"promise18_error" code:0 userInfo:nil]];
            FBLPromise *promise18_1 = [promise18 catch:^(NSError * _Nonnull error) {
                NSLog(@"error:%@", error);
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", promise18_1);
            });
    输出:
    error:Error Domain=promise18_error Code=0 "(null)"
    <FBLPromise 0x600001a62b50> Rejected: Error Domain=promise18_error Code=0 "(null)"
    

    FBLPromise+Recover.h

    @interface FBLPromise<Value>(RecoverAdditions)
    
    //创建一个待处理承诺
    //若原承诺为已拒绝承诺 异步调用recovery并根据recovery的返回值类型将待处理的承诺的状态改变
    //返回值类型为NSError 变成已拒绝 FBLPromise 变成同状态 其他 变成已实现
    //若原承诺为已实现承诺 不会调用recovery 异步将待处理承诺状态变成已实现
    - (FBLPromise *)recover:(FBLPromiseRecoverWorkBlock)recovery NS_SWIFT_UNAVAILABLE("");
    //error 已拒绝承诺的错误
    typedef id __nullable (^FBLPromiseRecoverWorkBlock)(NSError *error) 
    
    - (FBLPromise *)onQueue:(dispatch_queue_t)queue
                    recover:(FBLPromiseRecoverWorkBlock)recovery NS_REFINED_FOR_SWIFT;
    
    @end
    
    
    例:
            FBLPromise *promise19 = [FBLPromise pendingPromise];
    //        [promise19 fulfill:@"promise19"];
            [promise19 fulfill:[NSError errorWithDomain:@"promise19_error" code:0 userInfo:nil]];
            FBLPromise *promise19_1 = [promise19 recover:^id _Nullable(NSError * _Nonnull error) {
                NSLog(@"error:%@", error);
    //            return @"promise19";
                return [NSError errorWithDomain:@"promise19_1_error" code:0 userInfo:nil];
            }];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"%@", promise19_1);
            });
    输出:
    error:Error Domain=promise19_error Code=0 "(null)"
    <FBLPromise 0x600001b59680> Rejected: Error Domain=promise19_1_error Code=0 "(null)"
    若为
                return @"promise19";
    //            return [NSError errorWithDomain:@"promise19_1_error" code:0 userInfo:nil];
    输出
    error:Error Domain=promise19_error Code=0 "(null)"
    <FBLPromise 0x600002525e00> Fulfilled: promise19
    

    常见用法1

            NSLog(@"显示菊花");
            [[[[FBLPromise do:^id _Nullable{
                return [self doAction];
            }] then:^id _Nullable(id  _Nullable value) {
                NSLog(@"%@", value);
                return value;
            }] always:^{
                NSLog(@"隐藏菊花");
            }] catch:^(NSError * _Nonnull error) {
                NSLog(@"有报错 %@", error);
            }];
    
    - (FBLPromise *)doAction {
        FBLPromise *promise = [FBLPromise pendingPromise];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //3秒后接口调完
            [promise fulfill:@"promise"];
    //        [promise fulfill:[NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]];
        });
        return promise;
    }
    输出:
    显示菊花
    promise
    隐藏菊花
    
    若为
    //        [promise fulfill:@"promise"];
            [promise fulfill:[NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]];
    输出:
    显示菊花
    隐藏菊花
    有报错 Error Domain=promise_error Code=0 "(null)"
    

    常见用法2

            NSLog(@"显示菊花");
            [[[[FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
                [self asyncActionWithFulfillBlock:fulfill WithRejectBlock:reject];
            }] then:^id _Nullable(id  _Nullable value) {
                NSLog(@"%@", value);
                return value;
            }] always:^{
                NSLog(@"隐藏菊花");
            }] catch:^(NSError * _Nonnull error) {
                NSLog(@"有报错 %@", error);
            }];
    
    - (void)asyncActionWithFulfillBlock:(FBLPromiseFulfillBlock)fulfill WithRejectBlock:(FBLPromiseRejectBlock)Reject {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //3秒后接口调完
            fulfill(@"promise");
    //        fulfill([NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]);
    //        Reject([NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]);
        });
    }
    输出:
    显示菊花
    promise
    隐藏菊花
    
    若为
    //        fulfill(@"promise");
            fulfill([NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]);
    //        Reject([NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]);
    输出:
    显示菊花
    隐藏菊花
    有报错 Error Domain=promise_error Code=0 "(null)"
    

    常见用法3

            NSLog(@"显示菊花");
            [[[[FBLPromise async:^(FBLPromiseFulfillBlock  _Nonnull fulfill, FBLPromiseRejectBlock  _Nonnull reject) {
                [self asyncActionWithBlock:^(id value) {
                    fulfill(value);
                }];
            }] then:^id _Nullable(id  _Nullable value) {
                NSLog(@"%@", value);
                return value;
            }] always:^{
                NSLog(@"隐藏菊花");
            }] catch:^(NSError * _Nonnull error) {
                NSLog(@"有报错 %@", error);
            }];
    
    - (void)asyncActionWithBlock:(void (^)(id))block {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //3秒后接口调完
            block(@"promise");
    //        block([NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]);
        });
    }
    输出:
    显示菊花
    promise
    隐藏菊花
    
    若为
    //        block(@"promise");
            block([NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]);
    输出:
    显示菊花
    隐藏菊花
    有报错 Error Domain=promise_error Code=0 "(null)"
    

    常见用法4

            NSLog(@"显示菊花");
            [[[[FBLPromise wrapObjectOrErrorCompletion:^(FBLPromiseObjectOrErrorCompletion  _Nonnull handler) {
                [self wrapActionWithBlock:handler];
            }] then:^id _Nullable(id  _Nullable value) {
                NSLog(@"%@", value);
                return value;
            }] always:^{
                NSLog(@"隐藏菊花");
            }] catch:^(NSError * _Nonnull error) {
                NSLog(@"有报错 %@", error);
            }];
    
    - (void)wrapActionWithBlock:(FBLPromiseObjectOrErrorCompletion)block {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            //3秒后接口调完
            block(@"promise", nil);
    //        block(@"promise", [NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]);
        });
    }
    输出:
    显示菊花
    promise
    隐藏菊花
    
    若为
    //        block(@"promise", nil);
            block(@"promise", [NSError errorWithDomain:@"promise_error" code:0 userInfo:nil]);
    输出:
    显示菊花
    隐藏菊花
    有报错 Error Domain=promise_error Code=0 "(null)"
    

    相关文章

      网友评论

        本文标题:PromisesObjC

        本文链接:https://www.haomeiwen.com/subject/ihocqktx.html