KoGor
KoGor

Reputation: 237

Error handling practices with queue.js

I want to handle errors in a proper way when using queue.js, but have some uncertainty about how to make it.

For example we have such code:

function getImage(path, callback) {
  var img = new Image();
  img.src = path;
  //I have some issues about error object here.
  //What should we place in callback(???) to get internal error object with type and message?
  //Is it good practise to write: If (img.complete)?
  if (img.complete) callback(null, img);
  else callback(alert("Image loading fail")); //<- If we store here just text, it won't be available in function ready()
}

queue()
.defer(getImage, "image1.png")
.defer(getImage, "image2.png")
.await(ready);

function ready(error, img1, img2) {
  //How to handle errors properly on this stage?
  if (error !== null) {
    alert("Something wrong!"); // <- This doesn't work when path to image is wrong
  } else {
    //Do some cool stuff
  }
  console.log(error); // <- always show null whatever I store in callback for error case in function getImage()
}

The question is about handling errors in general when using queue.js, not only in scope of this example. So all you experience and practices are welcome.


Edit: Answer is readable there https://stackoverflow.com/a/18086989/1974961

Upvotes: 2

Views: 475

Answers (0)

Related Questions