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"
No comments:
Post a Comment