After java asynchrony in js is a problem for me. The essence of the problem: write a function that checks for the presence of a file, if there is, then true , no - false . It would seem simple, but it was not there.
Used the deprecated function. And it spawned a lot of code with its use. Now we need to make it so that the same function works the same way, but was implemented differently.
Now my code in if is executed as false , without waiting for the correct answer from doesFileExist(urlToFile); Tell me, how can I screw my legs here? With $.when I can't do anything (
function doesFileExist(urlToFile) { $.ajax({ url: urlToFile, type: 'HEAD', error: function () { console.log(false) return false; }, success: function () { console.log(true) return true; } }); } if(doesFileExist(url)){} old version
function doesFileExist(urlToFile){ var xhr = new XMLHttpRequest(); xhr.open('HEAD', urlToFile, false); xhr.send(); if (xhr.status == "404") { return false; } else { return true; } }
returntoerrorandsuccesshandlers does not make sense. The code that callsdoesFileExistwill still have to be redone. - Igor