title: iOS Swift Combine: Emit Publisher with single value
body: <p>I'm using Combine and it happens to me many times that I have the need to emit Publishers with single values.</p>
  
  <p>For example when I use flat map and I have to return a Publisher with a single value as an error or a single object I use this code, and it works very well:</p>
  
  <pre><code>return AnyPublisher&lt;Data, StoreError&gt;.init(
             Result&lt;Data, StoreError&gt;.Publisher(.cantDownloadProfileImage)
          )
  </code></pre>
  
  <p>This creates an AnyPublisher of type <code>&lt;Data, StoreError&gt;</code> and emits an error, in this case: <code>.cantDownloadProfileImage</code></p>
  
  <p>Here a full example how may usages of this chunk of code.</p>
  
  <pre><code>func downloadUserProfilePhoto(user: User) -&gt; AnyPublisher&lt;UIImage?, StoreError&gt; {
          guard let urlString = user.imageURL,
              let url = URL(string: urlString)
              else {
                  return AnyPublisher&lt;UIImage?, StoreError&gt;
                      .init(Result&lt;UIImage?, StoreError&gt;
                          .Publisher(nil))
          }
          return NetworkService.getData(url: url)
              .catch({ (_) -&gt; AnyPublisher&lt;Data, StoreError&gt; in
                  return AnyPublisher&lt;Data, StoreError&gt;
                      .init(Result&lt;Data, StoreError&gt;
                          .Publisher(.cantDownloadProfileImage))
              })
              .flatMap { data -&gt; AnyPublisher&lt;UIImage?, StoreError&gt; in
                  guard let image = UIImage(data: data) else {
                      return AnyPublisher&lt;UIImage?, StoreError&gt;
                          .init(Result&lt;UIImage?, StoreError&gt;.Publisher(.cantDownloadProfileImage))
                  }
                  return AnyPublisher&lt;UIImage?, StoreError&gt;
                      .init(Result&lt;UIImage?, StoreError&gt;.Publisher(image))
          }
          .eraseToAnyPublisher()
      }
  </code></pre>
  
  <p>Is there an easier and shorter way to create an AnyPublisher with a single value inside?</p>
  
  <p>I think I should use the <code>Just()</code> object in somehow, but I can't understand how, because the documentation at this stage is very unclear.</p>