Skip to content

Commit

Permalink
Merge pull request #33 from qhua948/master
Browse files Browse the repository at this point in the history
Added a new `recover` method to allow return of promises
  • Loading branch information
s4cha authored Feb 1, 2018
2 parents 52373d7 + 24708da commit ca7648d
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Source/Promise+Recover.swift
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ extension Promise {
}, progress: p.setProgress)
return p
}

public func recover(_ block:@escaping (Error) throws -> Promise<T>) -> Promise<T> {
let p = newLinkedPromise()
syncStateWithCallBacks(
success: p.fulfill,
failure: { e in
do {
let promise = try block(e)
promise.then { t in
p.fulfill(t)
}.onError { error in
p.reject(error)
}
} catch {
p.reject(error)
}
}, progress: p.setProgress)
return p
}

}

// Credits to Quick/Nimble for how to compare Errors
Expand Down

0 comments on commit ca7648d

Please sign in to comment.