0

I am using npm libraries to

  1. Download a pdf file
  2. 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.

  • if it returns a promise use `.then()` to get data .... [refer this link for more](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then) – KcH Feb 18 '21 at 11:52
  • Think about where you are returning that promise **to**, i.e. the function which actually **calls** the callback (it's isn't anywhere that can make use of it) – Quentin Feb 18 '21 at 11:53
  • As @Codenewbie mentioned, you can use `.then()` and provide it a callback to be executed when the promise is resolved or, if you're running this code in an envrionment that supports the ECMA2017 specification, you can use `async...await`. – sandmann Feb 18 '21 at 11:57
  • Also, @Quentin is right. When you're using callbacks, you have to think about where they're being executed, as its return value may not be used at all. You can however, define a variable (`telegraphCallbackReturn` for example) in the same scope you call `download` and assign `telegraphData` to it instead of returning it from the callback. If `telegraphData` is a promise, you can then call `telegraphCallbackReturn.then((value) => { /* do something with value */ })` or write `const value = await telegraphCallbackReturn` – sandmann Feb 18 '21 at 12:09
  • Thank you for all your answers! I have rephrased the question, but your comments helped me to get on a right track. Cheers! –  Feb 18 '21 at 12:15

0 Answers0