我想在使用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 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()
}
问题是关于在使用 queue.js 时处理错误的一般情况,而不仅仅是在本示例的范围内。因此,欢迎您的所有经验和实践。