Javascript queue.js的错误处理实践

Javascript queue.js的错误处理实践,javascript,error-handling,queue.js,Javascript,Error Handling,Queue.js,我想在使用时以适当的方式处理错误,但对如何使用它有些不确定 例如,我们有这样的代码: 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

我想在使用时以适当的方式处理错误,但对如何使用它有些不确定

例如,我们有这样的代码:

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()
}
函数getImage(路径,回调){ var img=新图像(); img.src=路径; //我这里有一些关于错误对象的问题。 //我们应该在回调(??)中放置什么来获取类型和消息为的内部错误对象? //写:If(img.complete)是一种很好的练习吗? if(img.complete)回调(null,img); else callback(alert(“Image loading fail”);//我想我(间接地)在我的代码示例中回答了这个问题:但这可能主要是基于对最佳实践的看法。