google / promises

Promises is a modern framework that provides a synchronization construct for Swift and Objective-C.
Apache License 2.0
3.81k stars 295 forks source link

recover return error or nil #38

Closed imunique-ZJ closed 6 years ago

imunique-ZJ commented 6 years ago

Hi, I'd like to use "recover" to catch errors and do some fallback function for specific error code. But I got errors in the recover block:

Return type 'NSError *' must match previous return type 'FBLPromise<NSMutableArray *> *' when block literal has unspecified explicit return type

Here's my sample.

[self doAWork]
.recover(^(NSError *error) {
    if (error.code == USE_FALLBACK) {
        return [self doFallback];
    } else {
        return error; // this will cause compile errors
    }
})
.then(^id(NSMutableArray *values) {
    return [self doBWork:values];
}).catch(^(NSError *error) {
    NSLog("something wrong. error= %@", error);
});

Is it possible to return error in "recover" block? Or are there any other ways I can try? Thanks.

shoumikhin commented 6 years ago

Don’t forget the recovery block prototype must have a return type of id. It’s missing in your snippet, so the compiler has valid complains. Use this: .recover(^id(NSError *error) {

imunique-ZJ commented 6 years ago

Oh, my bad. Thanks a lot. :)