title: Waiting in JavaScript beyond wrapping everything in setTimeout
body: <p>So, I'm creating a bot to play a video game online (completely legally, there's a server for bot competitions, don't worry). The code is in JavaScript, which I'm just picking up now, and I'm having trouble telling the bot to do something, then wait either by time or by condition until doing something else.</p>
  
  <p>So far, I'm simply wrapping everything in gigantic setTimeout commands, but this is extremely inelegant.</p>
  
  <p>There's not too much code that's relevant to post, but essentially I've made up a lot of code that does things like move to a specific location, brake, chase another player, etc. Most work with a recursive setTimeout command - press certain buttons, reset variables, then start the function again if the condition isn't met. If I want to move to a specific location then brake when I finish with that, generally I'd put code likes this:</p>
  
  <pre><code>moveTarget(target);
  brake();
  [...Execute rest of code...]
  </code></pre>
  
  <p>However, JavaScript executes both of these commands at the same time, which obviously doesn't work very well. Is there a way to make something more like this (Or the equivalent with a condition instead of a specified time):</p>
  
  <pre><code>moveTarget(target);
  wait3Seconds();
  brake();
  [...Execute rest of code...]
  </code></pre>
  
  <p>Without needing to do this:</p>
  
  <pre><code>moveTarget(target);
  setTimeout(function(){
      brake();
      [...Execute rest of code...]
  },3000);
  </code></pre>
  
  <p>Given how many of these situations I'll have, my code will end up looking like a gigantic mess of nested setTimeouts, which is not very pretty. Is there a better way? I'm already using JQuery, so I'm perfectly willing to use that if necessary.</p>