title: How do I make Javascript wait for one function to finish before moving to another?
body: <p>I'm fairly new to Javascript, and I need help with making my code wait for functions to finish before moving on to the next. In my code, I'm sending an email using SES, and I'm publishing a topic within SNS. </p>
  
  <p>After sending the email and publishing the topic, I need to navigate the user to a thank you page. </p>
  
  <p>With how I have my code setup, it's very inconsistent. Sometimes it will send the the email and publish before navigating, and sometimes it will do one or the other then navigate. I need help with understanding how to make the code wait for those two functions to finish before navigating to a different page.</p>
  
  <p>Thank you all in advance!</p>
  
  <p>I've tried using an if statement, but it didn't work out as I thought. </p>
  
  <pre><code>sns.publish(snsParams, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
  });
  ses.sendEmail(sesParams, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
  });
  window.location = 'https://abc123.com/contact/thanks/';
  </code></pre>