0daycode
0daycode

Reputation: 21

Using NodeJS promises for waiting the process of conversion completed

I've been trying for 1 week without success to render the psd conversion through this nodejs module: https://www.npmjs.com/package/psd trying to make a confirmation message appear after converting all the images. I don't know if the problem can be traced to my code or promise compliance, I've tried like 50 times to change every aspect of this code.

In psdconverter.js file

//! PSD COMPONENT MODULES
var PSD = require('psd');
//! file and extension MODULES
const fs = require('fs');
var path = require('path');


const PSDconverter = async (filename) => {
  return new Promise((resolve, reject) => {
    PSD.open('./img/' + filename).then(function (psd) {
      let newfilename = filename.replace(/.psd/i, ""); //REPLACE CASE INSENSITIVE
      psd.image.saveAsPng('./img/' + newfilename + '.png');
      return newfilename;
    }).then(function (res) {
      console.log('PSD Conversion Finished!' + res);
      resolve(res);
    }).catch(function (err) {
      console.log(err);
    });
  })
}


const EnumAndConvert = async () => {
  return new Promise((resolve, reject) => {
    //! READ DIR IMAGE AND CONVERTION PART
    fs.readdir('./img/', (err, files) => {
      if (err)
        console.log(err + ' errore di conversione, non è stata trovata la cartella img!');
      else {
        for (let filename of files){
          var ext = path.extname('./img/' + filename);
          if (ext === '.PSD' || ext === '.psd')
          await PSDconverter(filename);
        }

      }
    })
    resolve("Everything is converted successfully");   

  })
  
}
exports.PSDconverter = PSDconverter;
exports.EnumAndConvert = EnumAndConvert;

in index.js file

function PSDconverter() {
  //! convertitore PSD
  let EnumPSDAndConvert = require('./psdconverter.js');
  EnumPSDAndConvert.EnumAndConvert().then((res) => {  //dopo che è ritornata la Promise risolta continuo
    console.log(res+"ciao");
  })
}

ERROR RESULT: await PSDconverter(filename); ^^^^^

SyntaxError: await is only valid in async function

When i want the first to be the last one. Thank you for every help!

Upvotes: 0

Views: 111

Answers (1)

0daycode
0daycode

Reputation: 21

Ok the solution is: index.js

function PSDconverter() {
  //! convertitore PSD
  let EnumPSDAndConvert = require('./psdconverter.js');
  EnumPSDAndConvert.EnumAndConvert().then(() => { 
    console.log("Conversion Completed");
  })
}

psdconverter.js

//! PSD COMPONENT MODULES
var PSD = require('psd');
//! file and extension MODULES
const fs = require('fs');
var path = require('path');


const PSDconverter = (filename) => {  //without async 
  return PSD.open('./img/' + filename).then(function (psd) {
    let newfilename = filename.replace(/.psd/i, ""); //REPLACE CASE INSENSITIVE
    psd.image.saveAsPng('./img/' + newfilename + '.png');
    return newfilename;
  }).then(function (res) {
    console.log('PSD Conversion Finished!' + res);
  }).catch(function (err) {
    console.log(err);
  });
}

function readImgDir() {
  return new Promise((resolve, reject) => {
    fs.readdir('./img/', (err, files) => {
      if (err)
        console.log(err + ' errore di conversione, non è stata trovata la cartella img!');
      else {
        resolve(files);
      }
    })
    
  })
}
const EnumAndConvert = async () => {
  var files = await readImgDir();   //! READ DIR IMAGE AND CONVERTION PART
  for (let filename of files) {
    var ext = path.extname('./img/' + filename);
    if (ext === '.PSD' || ext === '.psd')
      await PSDconverter(filename);
  }

}
exports.PSDconverter = PSDconverter;
exports.EnumAndConvert = EnumAndConvert;

If there are any suggestions on how to improve the code I would be curious. Thanks Again

Upvotes: 1

Related Questions