title: Testing Asynchronous Callbacks with Jasmine
body: <p>I'm using Jasmine 2.1. I am trying to use Jasmine 2.1 to test a module. One of my modules has a function that executes code asynchronously. I need to test the result of the function when the app is done executing. Is there a way to do this? Currently, my module looks like this:</p>
  
  <pre><code>var otherModule = require('otherModule');
  function MyModule() {
  }
  
  MyModule.prototype.state = '';
  MyModule.prototype.execute = function(callback) {
    try {
      this.state = 'Executing';
      var m = new otherModule.Execute(function(err) {
        if (err) {
          this.state = 'Error';
          if (callback) {
            callback(err);
          }
        } else {
          this.state = 'Executed';
          if (callback) {
            callback(null);
          }
        }
      });
    } catch (ex) {
      this.state = 'Exception';
      if (callback) {
        callback(ex);
      }
    }
  };
  
  module.exports = MyModule;
  </code></pre>
  
  <p>I am trying to test my Module with the following:</p>
  
  <pre><code>var MyModule= require('./myModule');
  describe("My Module", function() {
    var myModule = new MyModule();
    it('Execute', function() {
      myModule.execute();
      expect(myModule.state).toBe('Executed');
    });
  });
  </code></pre>
  
  <p>Clearly, the test is not awaiting for the execution to occur. How do I test an asynchronous executed function via Jasmine? In addition, am I using the state variable properly? I get lost in the asynchronous stack and I'm unsure where I can use '<code>this</code>'.</p>