title: How do you sequentially chain observables in concise and readable way
body: <p>Im new to RXSwift and I've begun investigating how I can perform Promise like function chaining.</p>
  
  <p>I think I'm on the right track by using flatmap but my implementation is very difficult to read so I suspect theres a better way to accomplish it.</p>
  
  <p>What I have here seems to work but I'm getting a headache thinking about what It might looks like if I added another 3 or functions to the chain.</p>
  
  <p><strong>Here Is where I declare my 'promise chain'(hard to read)</strong></p>
  
  <pre><code>LOGIN().flatMap{ (stuff) -&gt; Observable&lt;Int&gt; in
      return API(webSiteData: stuff).flatMap
      { (username) -&gt; Observable&lt;ProfileResult&gt; in
      return accessProfile(userDisplayName: username) }
      }.subscribe(onNext: { event in
          print("The Chain Completed")
          print(event)
      }, onError:{ error in
           print("An error in the chain occurred")
      })
  </code></pre>
  
  <p><strong>These are the 3 sample functions I'm chaining</strong>  </p>
  
  <pre><code>struct apicreds
  {
      let websocket:String
      let token:String
  }
  typealias APIResult = String
  typealias ProfileResult = Int
  
  // FUNCTION 1
  func LOGIN() -&gt; Observable&lt;apicreds&gt; {
  
      return Observable.create { observer in
          print("IN LOGIn")
          observer.onNext(apicreds(websocket: "the web socket", token: "the token"))
          observer.on(.completed)
          return Disposables.create()
      }
  }
  // FUNCTION 2
  func API(webSiteData: apicreds) -&gt; Observable&lt;APIResult&gt; {
  
      return Observable.create { observer in
          print("IN API")
          print (webSiteData)
         // observer.onError(myerror.anError)
          observer.on(.next("This is the user name")) // assiging "1" just as an example, you may ignore
          observer.on(.completed)
          return Disposables.create()
      }
  
  }
  //FUNCTION 3
  func accessProfile(userDisplayName:String)  -&gt; Observable&lt;ProfileResult&gt;
  {
      return Observable.create { observer in
  
          // Place your second server access code
          print("IN Profile")
          print (userDisplayName)
  
          observer.on(.next(200)) // 200 response from profile call
          observer.on(.completed)
          return Disposables.create()
      }
  }
  </code></pre>