title: The transition from "fetching" to "displaying" JSON API data Swift Node.js
body: <p>I understand how to &quot;fetch&quot; data from a JSON API (my local server, in fact), but how should I think about the pipeline from merely having the data to displaying it in views? What I intuitively want to do is &quot;return&quot; the data from the fetching function, though I know that's not the paradigm that the Swift URL functions operate with. My thought is that if I can &quot;return&quot; the data (as a struct) it will be easy to pass into a view for visualization.</p>
  <p>Sample Code:</p>
  <p>This is the structure of the fetched JSON and the kind of variable I want to pass into views.</p>
  <pre><code>struct User: Codable {
      let userID: String
      let userName: String
      let firstName: String
      let lastName: String
      let fullName: String
  }
  </code></pre>
  <p>My hope is that the printUser function can return instead of print a successful fetch.</p>
  <pre><code>func printUser() {
      fetchUser { (res) in
          switch res {
          case .success(let user):
              print(user.userName) // return here? 
                                // I know it won't work, but that's what seems natural
          case .failure(let err):
              print(&quot;Failed to fetch user: &quot;, err)
          }
      }
  }
  
  func fetchUser(completion: @escaping (Result&lt;User, Error&gt;) -&gt; ()) {
      let urlString = &quot;http://localhost:4001/user&quot;
      guard let url = URL(string: urlString) else { return }
      URLSession.shared.dataTask(with: url) { (data, resp, err) in
      
          if let err = err {
              completion(.failure(err))
              return
          }
      
          do {
              let user = try JSONDecoder().decode(User.self, from: data!)
              completion(.success(user))
          
          } catch let jsonError {
              completion(.failure(jsonError))
          }
  
      }.resume()
  }
  </code></pre>
  <p>Sample view that would take a user struct</p>
  <pre><code>struct DisplayUser: View {
      var user: User
      var body: some View {
          Text(user.userID)
          Text(user.userName)
          Text(user.lastName)
          Text(user.firstName)
          Text(user.fullName)
      }
  }
  </code></pre>