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.
After sending the email and publishing the topic, I need to navigate the user to a thank you page.
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.
Thank you all in advance!
I've tried using an if statement, but it didn't work out as I thought.
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/';