Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to run a function after map in Electron

I am encrypting an array of files and using node streams to encrypt a file in chunk. I want to run a function after the encryption is done of all the files in the array but my function runs before the encryption has completed. I am using preload.js to expose the encryption function.

//encrypt.js
const path = require("path");
const fs = require("fs");
const { pipeline } = require("stream/promises");
const { app } = require("./app.js");

async function encrypt(file) {
  const fileReadStream = fs.createReadStream(file);

  const filePath = path.parse(file).dir;
  const fileName = path.parse(file).name;
  const fileExt = path.parse(file).ext;
  const EncFile = filePath + "/" + fileName + "_enc" + fileExt;
  const fileWriteStream = fs.createWriteStream(EncFile);

  await pipeline(
    fileReadStream,
    new Transform({
      transform(chunk, encoding, callback) {
        const encryptedData = app.encrypt(chunk, password);
        callback(null, encryptedData);
        console.log("File encrypted");
      },
    }),
    fileWriteStream
  );
}
module.exports.encrypt = encrypt;
//preload.js
const { encrypt } = require('./encrypt.js');
contextBridge.exposeInMainWorld('encrypt', encrypt);
const para = document.querySelector('#para-info');
const btn = document.querySelector('#btn');
btn.addEventListener("click", () => {
  filesList.map(file => {
    window.encrypt(file, password);
  });
  done();
}

// function i want to run after encryption is done
function done(){
  para.innerText = 'Encryption Done';
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

The function is async:

async function encrypt(file) {

But it’s not being awaited:

filesList.map(file => {
  window.encrypt(file, password);
});
done();

One approach is to do all this in an async function to make use of await:

let myFunc = async () => {
  for (let file of filesList) {
    await window.encrypt(file, password);
  }
  done();
};
myFunc();

Or capture the resulting Promise objects and wait for all of them:

let promises = filesList.map(file => {
  return window.encrypt(file, password);
});
Promise.all(promises).then(() => done());
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading