title: Combine Future block called multiple times when using Flatmap and multiple subscribers
body: <p>I've been successfully using <a href="https://github.com/Thomvis/BrightFutures" rel="nofollow noreferrer">BrightFutures</a> in my apps mainly for async network requests. I decided it was time to see if I could migrate to <a href="https://developer.apple.com/documentation/combine" rel="nofollow noreferrer">Combine</a>. However what I find is that when I combine two <a href="https://developer.apple.com/documentation/combine/future" rel="nofollow noreferrer">Futures</a> using <a href="https://developer.apple.com/documentation/combine/future/3333364-flatmap" rel="nofollow noreferrer">flatMap</a> with two subscribers my second <a href="https://developer.apple.com/documentation/combine/future" rel="nofollow noreferrer">Future</a> code block is executed twice. Here's some example code which will run directly in a playground:</p>
  
  <pre class="lang-swift prettyprint-override"><code>import Combine
  import Foundation
  
  extension Publisher {
      func showActivityIndicatorWhileWaiting(message: String) -&gt; AnyCancellable {
          let cancellable = sink(receiveCompletion: { _ in Swift.print("Hide activity indicator") }, receiveValue: { (_) in })
          Swift.print("Busy: \(message)")
          return cancellable
      }
  }
  
  enum ServerErrors: Error {
      case authenticationFailed
      case noConnection
      case timeout
  }
  
  func authenticate(username: String, password: String) -&gt; Future&lt;Bool, ServerErrors&gt; {
      Future { promise in
          print("Calling server to authenticate")
          DispatchQueue.main.async {
              promise(.success(true))
          }
      }
  }
  
  func downloadUserInfo(username: String) -&gt; Future&lt;String, ServerErrors&gt; {
      Future { promise in
          print("Downloading user info")
          DispatchQueue.main.async {
              promise(.success("decoded user data"))
          }
      }
  }
  
  func authenticateAndDownloadUserInfo(username: String, password: String) -&gt; some Publisher {
      return authenticate(username: username, password: password).flatMap { (isAuthenticated) -&gt; Future&lt;String, ServerErrors&gt; in
          guard isAuthenticated else {
              return Future {$0(.failure(.authenticationFailed)) }
          }
          return downloadUserInfo(username: username)
      }
  }
  
  let future = authenticateAndDownloadUserInfo(username: "stack", password: "overflow")
  let cancellable2 = future.showActivityIndicatorWhileWaiting(message: "Please wait downloading")
  let cancellable1 = future.sink(receiveCompletion: { (completion) in
      switch completion {
      case .finished:
          print("Completed without errors.")
      case .failure(let error):
          print("received error: '\(error)'")
      }
  }) { (output) in
      print("received userInfo: '\(output)'")
  }
  </code></pre>
  
  <p>The code simulates making two network calls and <code>flatmap</code>s them together as a unit which either succeeds or fails.
  The resulting output is:</p>
  
  <blockquote>
    <p><code>Calling server to authenticate</code><br>
    <code>Busy: Please wait downloading</code><br>
    <code>Downloading user info</code><br>
    <code>Downloading user info</code> &nbsp;&nbsp;&nbsp;&nbsp;&lt;---- unexpected second network call <br>
    <code>Hide activity indicator</code><br>
    <code>received userInfo: 'decoded user data'</code><br>
    <code>Completed without errors.</code></p>
  </blockquote>
  
  <p>The problem is <code>downloadUserInfo((username:)</code> appears to be called twice. If I only have one subscriber then <code>downloadUserInfo((username:)</code> is only called once. I have an ugly solution that wraps the <code>flatMap</code> in another <code>Future</code> but feel I missing something simple. Any thoughts?</p>