xxxxxxxxxx
31
function loadImageWithPromise(imageUrl) {
return new Promise((resolve, reject) => {
loadImage(imageUrl, (img) => {
if (img) {
resolve(img);
} else {
reject("image not loading");
}
});
});
}
function setup() {
createCanvas(400, 400);
// with the default loadImage function
loadImage("MrBubz.jpg", (img) => {
image(img, 0, 0, 200, 200);
});
// with our custom promisified version
loadImageWithPromise("MrBubz.jpg")
.then((result) => {
image(result, 200, 200, 200, 200);
})
.catch((err) => {
console.error(new Error("Unable to load image"));
return err;
});
}