I can write a callback function that will halt execution of one script until another is finished. I found it here.
dosomething(4, function () {
console.log("finished loop");
});
function dosomething(delay, callback) {
for (var i = 0; i < 100; i++) {
$("body").append(i + "<br>");
}
alert("done");
if (typeof callback == "function") callback();
}
However, this will not work with functions that are asynchronous. I think that's the reason.
doAjax(4, function () {
console.log("finished ajax");
});
function doAjax(delay, callback) {
$.ajax({
type: "post",
url: "/echo/html/",
data: {
html: "some content",
delay: 4
},
dataType: 'html',
success: function () {
console.log("ajax done");
}
});
if (typeof callback == "function") callback();
}
I know it is a bit silly to ask how to incorporate my own callback for an asynchronous request since jQuery has them built in and I just used one, but I was wondering how it could be achieved. Is it as complex as writing an entire promise library?