I have tried many variations of getting my function to return a param so that I can run my functions only when one step is complete but looking for the best way that would not get me into call back hell.
I have the following function
function convertPdfToImage(fileName) {
return new Promise((resolve, reject) => {
var pdfImage = new PDFImage(fileName+".pdf");
pdfImage.convertFile().then(function (imagePaths) {
var file = fs.createWriteStream(imagePaths);
file.end();
file.on("finish", () => { resolve(true); });
file.on("error", reject);
return resolve(true)
});
});
}
Which I am calling here like this:
Promise.all([convertPdfToImage('test2'), convertPdfToImage('test1')])})
.then(function () {
compareItems(codeId);
});
This is the method called in the PromiseAll
function convertPdfToImage(env: string, shareClassId: string[]) {
return new Promise((resolve, reject) => {
var pdfImage = new PDFImage("output/" + shareClassId + env + ".pdf");
pdfImage.convertFile().then(function (imagePaths) {
var file = fs.createWriteStream(imagePaths);
file.end();
file.on("finish", () => { resolve(true); });
file.on("error", reject);
return resolve(true)
});
});
}
The problem no matter what I try, I cannot guarantee that compareItems runs after convertPdfToImage (PromiseAll has resolved)