0

I'm integrating PromiseKit into our current system and I need the catch part of the chain to use 2 arguments. I'd like my catch to use error, fetcher instead of just error. What is the best way to do this?

infoPromise.then { objects in
    print(objects)
}.catch { error in /* error, fetcher in */
    print(error)
}

I've thought of including the fetcher as part of the userInfo dictionary on NSError, but I'd prefer it as a separate argument if possible.

Edit - More Info:

Here is the adapter I'm using. The fetcher is returned in the onError from my existing system.

- (AnyPromise *)fetchAll:(NSString *)entityName {
    return [AnyPromise promiseWithAdapterBlock:^(PMKAdapter  _Nonnull adapter) {
        [self fetchAll:entityName onComplete:^(NSArray *results) {
            adapter(results,nil);
        } onError:^(ICSHTTPFetcher *fetcher, NSError *error) {
            adapter(nil,error);
        }];
    }];
}
Alex
  • 3,861
  • 5
  • 28
  • 44

1 Answers1

1

I have not used promises much in Objective C. But if you are able to write the fetchAll function in swift then you could wrap the error and fetcher in a Swift error. Below is a rough example of this - it won't compile, it's just to give you an idea. It would probably be better to make a few more specific PromiseErrors indicating the actual error, than just the one as I have done and then not pass the NSError through.

enum PromiseErrors: Error {
    case fetchAll(NSError, ICSHTTPFetcher)
}

struct Test {
    func fetchAll() -> Promise<Any> {
        return fetchAll(entityName, onComplete: { results in
            return Promise(value: results)
        }, error: { fetcher, error in
            return Promise(error: PromiseErrors.fetchAll(error, fetcher))
        })
    }

    func test() {
        infoPromise.then { objects in
            print(objects)
        }.catch { error in
            if let case PromiseErrors.fetchAll(error, fetcher) = error {

            }
        }
    }
}
totiDev
  • 5,189
  • 3
  • 30
  • 30
  • This seems like a good solution for Swift, thank you for the answer. But, I need to have it written in Obj-C so for now I'm just going to return the fetcher inside the userInfo dictionary of NSError. – Alex Jun 15 '17 at 14:19
  • this example seems strange to me! Normally a function returns it's promise immediately while some async task does it's work.
        func fetchAll() -> Promise {
            return fetchAll(entityName, onComplete: { results in
                return Promise(value: results)
    <\pre>
    in this case, the call to fetchAll() will block until the async task completes & yields its result....
    – Dewey Jul 31 '17 at 04:23