In short, you can't.
There is no way to force asynchronous functions to be synchronous. Instead, if you have something asynchronous, you have to build all of your code to handle that.
Your best bet is just to embrace the asynchronicity and build it in to handle it:
$.get('', function () {
console.log(1);
}).then(function () {
console.log(2);
});
In the long run, that'll cause you the fewest headaches.
There are other ways to deal with it, like using values to track if certain things are done and then set timeouts to check periodically:
let ready = false;
$.get('', function () {
ready = true;
console.log(1);
});
function checkReady() {
if (ready) {
console.log(2);
} else {
setTimeout(checkReady, 500);
}
}
checkReady();
This will check every 500ms if it's ready before running. This will let you kind of hide the async nature of your code. This can work in scenarios where you have very, very few async operations, but it can also cause confusion and weird bugs if you aren't careful, so use it carefully.