title: Creating a Q promise and invoking it later
body: <p>I'm trying to create a dialog box system in typescript. </p>
  
  <p>The intended use would be that a caller would do something like this;</p>
  
  <pre><code>dialogBox.showDialog().then((result: DialogResult) =&gt; {
      // Handle the dialog result
  });
  </code></pre>
  
  <p>My <code>DialogBox</code> class would probably have some methods like this;</p>
  
  <pre><code>private promise : Q.Promise&lt;DialogResult&gt;; 
  
  public showDialog() : Q.Promise&lt;DialogResult&gt;{
      this.promise = ... // How to create this promise?
  
      return this.promise;
  }
  
  public void setResult(result : DialogResult){
      // What to do here?
  }
  </code></pre>
  
  <p>Whenever the user clicks a button in the dialog, something like this would be called;</p>
  
  <pre><code>dialogBox.setResult(theResult);
  </code></pre>
  
  <p>This should then resolve/fulfill the promise created by the <code>showDialog</code> method. </p>
  
  <p>But I can't really figure out if this is possible with Q and so, how to implement the (promise-related parts of) <code>showDialog</code> and <code>setResult</code>. Anyone got any ideas?</p>
  
  <p><strong>Update for completeness</strong>; Thanks to Bergi, here's my final, working code. Ended up using a deferred</p>
  
  <pre><code>export class DialogBox implements INotification {
      private deferred: Q.Deferred&lt;DialogResult&gt;; 
  
      constructor(public message: string,
                  public header: string,
                  public buttons?: DialogResult[]) {
      }
  
      public showDialog(): Q.Promise&lt;DialogResult&gt; {
          this.deferred = Q.defer&lt;DialogResult&gt;();
  
          // My logic for displaying the box goes here
  
          return this.deferred.promise;
      }
  
      public setResult(result: DialogResult) {
          this.deferred.resolve(result);
      }
  }
  </code></pre>