I am totally new to firefox extensions and javascript. I have something like this:
var json = JSON.parse(links);
for (var i = 0; i < json.length; i++) {
var link = json[i];
Request({
url: link.url,
onComplete: function (response) {
console.log(response.status);
}
}).get();
}
Now I need to do something based on a status of a response object. But I would like to continue in my for loop because I need that link object...
On documentation page of Request https://developer.mozilla.org/en-US/Add-ons/SDK/High-Level_APIs/request is mentioned 'complete' event which is emitted after response being returned but I really don't know how to listen for events like this.
I really don't know how to approach this but this is result I need:
var json = JSON.parse(links);
for (var i = 0; i < json.length; i++) {
var link = json[i];
Request({
url: link.url,
onComplete: function (response) {
// something
}
}).get();
// I need RESPONSE object here
if(response.status == 404) {
// do something with link object
}
}
onComplete
is invoked asynchronously. For cases like these you need to 'save' the reference to a link object in a closure.
var fetchAndProcess = function(link) {
Request({
url: link.url,
onComplete: function(response) {
if (response.status == 404) {
console.log(link); // 'saved' in fetchAndProcess
// do something with link object
}
}
}).get();
};
var json = JSON.parse(links);
for (var link of json) {
fetchAndProcess(link);
}