I have a very basic XMLHttpRequest function that would be used with web workers.
I would like to know how I could improve it to handle possible errors better:
var ThreadedXhrRequest = function(url) {
var worker = new Worker("xhrWorker.js");
worker.postMessage([url]);
worker.onmessage = function(d) {
console.log(d.data)
}
}
xhrWorker.js
onmessage = function(e) {
var xhr = new XMLHttpRequest();
xhr.open("GET", e[0], true);
xhr.overrideMimeType("text/plain; charset=x-user-defined");
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
postMessage(xhr.responseText);
close();
}
}
xhr.send(null);
}
worker.onmessage
function rather than calling it. Without seeing whatpostMessage()
does, it would appear that this code wouldn't actually do anything. Consider adding more context to your code in post. \$\endgroup\$