I have a synchronous function myFunction
in which I need to retrieve the contents of a website. However, the async
parameter I’m currently using is deprecated and I’m only aware of async AJAX requests. Thus I’m looking for a better solution how I can wait for the asynchronous call to finish and process the result synchronously.
The myFunction
function is embedded in a library that I cannot modify. Thus there are the following limitations:
- I cannot turn
myFunction
into a async function - I cannot use any third-party libraries, only plain javascript and jQuery
var myFunction = function(url) {
var mydata = false;
$.get(
{
url: url,
async: false
})
.done(content => {
// Do something with content.
if(content === 'xyz') {
mydata = content;
return;
}
});
return mydata;
};
I have already looked into Promises and async/await but I couldn’t find a good solution for wrapping async calls. I also didn’t found another method that can make synchronous Get requests. What’s the best way to implement this?