R
Reactiflux

help-js

lxd – 15-40 Apr 14

LLilyyyXD4/14/2023
How to use Async properly in my case (code below) 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
LLilyyyXD4/14/2023
EEva4/14/2023
Use fs/promises, would be way more simpler than using callback API
LLilyyyXD4/14/2023
Any hint how to convert this code to promise, only a little bit
EEva4/14/2023
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');
UUUnknown User4/16/2023
Message Not Public
Sign In & Join Server To View

Looking for more? Join the community!