I am reading a local XML file and outputing the results as variables and i want to check that these are "available" else produce the error. Currently this code is saying that z equals empty, but it is not. Basically I want this to check each one in turn (check z, c, f in this order), and if any of them are empty produce the error message.
I am relatively new to promises so direction is really appreciated.Code:<script> var promise = new Promise(function(resolve, reject){ var xmlhttp = new XMLHttpRequest(); xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { myFunction(xmlhttp); } }; xmlhttp.open("GET", "cd_catalog.xml", true); xmlhttp.send(); function myFunction(xml) { var xmlDoc = xml.responseXML; x = xmlDoc.getElementsByTagName("IP")[0]; y = x.childNodes[0]; z = y.nodeValue; console.log(z); a = xmlDoc.getElementsByTagName("CountryName")[0]; b = a.childNodes[0]; c = b.nodeValue; console.log(c); d = xmlDoc.getElementsByTagName("City")[0]; e = d.childNodes[0]; f = e.nodeValue; console.log(f); } if(myFunction(z == "")){ resolve("Stuff worked"); } else{ reject(new Error("It broke")); } }); promise .then(result => { console.log(y + " " + "This is awesome" + " " + y * 5); }) .catch(error => { fallbackimg = new Image(); fallbackimg.src = "http://3.bp.blogspot.com/-fxkwVPW57DY/UBfvtElw15I/AAAAAAAAAMc/xwlCZJEZ2Lg/s1600/love-color-rain-Love-hearts-ngi-5-500x500-Bookmarks-406647-normal_large.png"; fallbackimg.alt =""; document.getElementById("demo").appendChild(fallbackimg); }); </script>
Thanks
Rustic



Reply With Quote
