I want to get some images, so I wrote a promise based script to get images from a chosen image url. However, when an invalid url is supplied it is supposed to return "Get Error." But instead (as I can see), the application crashes, and thus the try-catch block is not working.
const Jimp = require("jimp");
function rgba(source, width, height) {
return new Promise((resolve, reject) => {
try {
resolve((async () => {
const image = await Jimp.read(source);
const resized = image.resize(width, height);
const rgbaArray = [];
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
rgbaArray.push(Jimp.intToRGBA(resized.getPixelColor(x, y)));
};
};
return rgbaArray;
})());
} catch {
reject("Get Error");
};
});
};
// Returns correct result.
rgba(
"https://upload.wikimedia.org/wikipedia/commons/9/9e/SpaceX_Crew-1_Launch_%28NHQ202011150029%29.jpg",
50,
50
).then(resolve => {
console.log(resolve);
}).catch(reject => {
console.log(reject);
});
// App crashes instead of logging "Get Error."
rgba(
"improper_url",
50,
50
).then(resolve => {
console.log(resolve);
}).catch(reject => {
console.log(reject);
});