title: Do I need to be concerned with race conditions with asynchronous Javascript?
body: <p>Suppose I load some Flash movie that I know at some point in the future will call <code>window.flashReady</code> and will set <code>window.flashReadyTriggered = true</code>.</p>
  
  <p>Now I have a block of code that I want to have executed when the Flash is ready. I want it to execute it immediately if <code>window.flashReady</code> has already been called and I want to put it as the callback in <code>window.flashReady</code> if it has not yet been called. The naive approach is this:</p>
  
  <pre><code>if(window.flashReadyTriggered) {
    block();
  } else {
    window.flashReady = block;
  }
  </code></pre>
  
  <p>So the concern I have based on this is that the expression in the <code>if</code> condition is evaluated to <code>false</code>, but then before <code>block()</code> can be executed, <code>window.flashReady</code> is triggered by the external Flash. Consequently, <code>block</code> is never called.</p>
  
  <p>Is there a better design pattern to accomplish the higher level goal I'm going for (e.g., manually calling the <code>flashReady</code> callback)? If not, am I safe, or are there other things I should do?</p>