Welcome to EdListen:
Never Stop Learning

jQuery/Javascript Promise

JavaScript programming runs things in order as it reads the code, but it doesn't stop to wait for the previous operation to complete before moving on.   This improves performance, but there are times where you need a bit of code to complete before you run another function.    The solution to that is called a "promise".  Basically as the code is read it will see that one particular piece of code (function) promise's to wait till another piece is done.

I use jQuery when I code, and the method I found that works best for myself is to use $.when().then(); statement.

Below is a quick example of how I format the $.when().then(); statement.

var promiseFunction = function(){ console.log("run this first") }; 
//You can include this inside the "when" brackets, but I found it cleaner to keep it separated.  This will run right away.
$.when( promiseFunction ).then(function(){  console.log("run after")  }); //The "then(function(){})" will wait till the promiseFunction is complete before running.
console.log("this will not wait for promiseFunction to complete and will run right away");
//The rest of the code will continue to run at the same time as "var promiseFunction"