I´m a making a pdf certificate and I am calling the function from the constructor of a class. The code works as intended but I need to convert it to a promise so I can get a notification and continue with other stuff. Right now If I write a console.log(“done”) at the end of the function the console.log appears before the process is ended. I know a promise is the solution but I don’t know how to implement it.
This is the code I have now but I need a promise for the makeCertificate function
class GenerateCertificate {
constructor(date, name, urlImg) {
this.urlImg = urlImg;
this.date = date;
this.name = name;
this.makeCertificate(this.date, this.name);
}
makeCertificate(date, name) {
const doc = new jsPDF({
orientation: 'landscape',
});
const img = new Image();
img.onload = function () {
doc.addImage(this, 0, 0, 297, 210);
doc.setFontSize(40);
doc.setTextColor(0, 0, 0);
doc.setFont('Courier', 'bold');
doc.text(
name,
doc.internal.pageSize.width / 2,
120,
null,
null,
'center',
);
doc.setFont('Courier', 'normal');
doc.setFontSize(15);
doc.text(
date,
doc.internal.pageSize.width / 2,
172,
null,
null,
'center',
);
doc.save('Certificate.pdf');
};
img.crossOrigin = '';
img.src = this.urlImg;
console.log('done'); //This is logged before the certificate is complete
}
}