lxd – 15-40 Apr 14

How to use Async properly in my case (code below)
L
LilyyyXD370d 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
L
LilyyyXD370d ago
E
Eva370d ago
Use fs/promises, would be way more simpler than using callback API
L
LilyyyXD370d ago
Any hint how to convert this code to promise, only a little bit
E
Eva370d 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');
UU
Unknown User368d ago