Skip to content

Commit

Permalink
Invalidate publisher promise on first fire
Browse files Browse the repository at this point in the history
  • Loading branch information
EricRabil committed Sep 11, 2021
1 parent 2b004b1 commit 5b6d3f3
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions Sources/Pwomise/Extensions/Publisher+Promise.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ public extension Publisher {
var promise: Promise<Output> {
let promise = Promise<Output>()

sink(receiveCompletion: {
var cancellable: AnyCancellable?

cancellable = sink(receiveCompletion: {
switch $0 {
case .failure(let err):
promise.result = .resolved(.failure(err))
cancellable?.cancel()
cancellable = nil
default:
break
}
}, receiveValue: { value in
promise.result = .resolved(.success(value))
}).store(in: &cancellables)
cancellable?.cancel()
cancellable = nil
})

return promise
}
Expand Down

0 comments on commit 5b6d3f3

Please sign in to comment.