LilyyyXD
LilyyyXD16mo ago

lxd – 15-40 Apr 14

How to use Async properly in my case (code below)
6 Replies
LilyyyXD
LilyyyXD16mo ago
var fs = require("fs"); var pdf2img = require('pdf-img-convert'); async function createImg() { pdfArray = await pdf2img.convert("input/pdf2.pdf"); for (i = 0; i < pdfArray.length; i++) { fs.writeFile("output/temp1.png", pdfArray[i], function (error) { console.log("1"); if (error) { console.error("Error: " + error); } }); //writeFile } // for } // createImg(); async function f(){ await createImg(); console.log("2"); } f(); output: 2 1 I want it to be 1 2
LilyyyXD
LilyyyXD16mo ago
Eva
Eva16mo ago
Use fs/promises, would be way more simpler than using callback API
LilyyyXD
LilyyyXD16mo ago
Any hint how to convert this code to promise, only a little bit
Eva
Eva16mo ago
const fs = require('fs/promises');

// ...
await fs.writeFile(path, content); // use .catch or try...catch with await
console.log('1');
const fs = require('fs/promises');

// ...
await fs.writeFile(path, content); // use .catch or try...catch with await
console.log('1');
reactibot
reactibot16mo ago
This thread hasn’t had any activity in 36 hours, so it’s now locked. Threads are closed automatically after 36 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/1096460083770175608