I am using npm libraries to
- Download a pdf file
- After it's downloaded, convert it to png
Both libraries provide callbacks. Here is my code :
download(url,{directory, filename: filename(day) } , function(err){
if (err) throw err;
converter({
input: `${day}.pdf`,
output: 'output.png',
scale: '1'
}, () => {
let formData = new FormData();
let headers;
formData.append("files", fs.createReadStream('./output.png'), { knownLength: fs.statSync('./output.png').size });
headers = {
...formData.getHeaders(),
"Content-Length": formData.getLengthSync()
};
const telegraphData = axios.post("https://telegra.ph/upload", formData, {headers}).then((result) => {
url = `https://telegra.ph${result.data[0].src}`;
typeof data !== 'string' ? ctx.replyWithPhoto(url, { caption: `${data.message}${data.additionalData}#koronavirus` }) : ctx.replyWithPhoto(url, { caption: `${message}` });
return url;
});
return telegraphData
})
});
My problem is as follows : I am returning a promise inside of callback of converter
function called telegraphData
. And I want access that promise, outside the converter
funtion. The converter
function returns undefined
when I try console.log it to see if promise if fulfilled or not. Attaching .then()
to converter
does not work. And it may be a simple thing, but I don't know what I am missing here. Please, point me in the right direction.