title: Implementing Promise with Channels in Go
body: <p>I'm trying to implement Promise in Go which would be similar to that in Javascript. </p>
  
  <pre><code>type Promise struct {
          Result chan string
          Error  chan error
  }
  
  func NewPromise() (*Promise) {
          r := make(chan string, 1)
          e := make(chan error, 1)
          return &amp;Promise{
                  Result: r,
                  Error:  e,
          }
  }
  
  func main() {
          var p = NewPromise()
  
          go func(p *Promise) {
                  time.Sleep(time.Duration(5)*time.Second)
                  p.Result &lt;- "done"
          }(p)
  
          if &lt;- p.Result {
                  fmt.Println(&lt;-p.Result)
          }
  
          // Is it possible to do something else here while wait for 5s?
  
          // Once Promise is fulfilled after 5s, the Result is available.
  }
  </code></pre>
  
  <p>How do I do the following:</p>
  
  <ol>
  <li>Run a goroutine, which return <code>Promise</code> to the main goroutine right
  away. </li>
  <li><p>Asynchronously do something on the main routine while wait for
  anything to be sent to either <code>Promise.Result</code> or <code>Promise.Error</code></p></li>
  <li><p>Once something is sent, return from the goroutine and make that
  channel available to be read.</p></li>
  </ol>