Node JS Promise.all to async update properties in array of JSON objects

356 views Asked by At

I'm new to JS and Node.js and I'm working on a personnal project to translate webVTT subtitle files using Azure Translator API - To do that, I use the node-webvtt npm package to parse / compile the webVTT file. The parsing operation provides a JSON object that contains an array of thousands of cues that looks like that :

[
      {
         "identifier":"",
         "start":60,
         "end":61,
         "text":"My text to be translated #1",
         "styles":""
      },
      {
         "identifier":"",
         "start":110,
         "end":111,
         "text":"My text to be translated #2",
         "styles":""
      }
]

To translate the "text" property, I use the code sample provide by Microsoft on GitHub and applied the following changes to the translateText function:

  • I create a promise that returns a "cue" object (and not only the translated text)
  • I take a "cue" object as input and the language to be translated to

Then I followed the code provided here, that uses a combination of Promise.all and item.map to translate all the cues text via my async translateText function

Here is the code of my index.js - It works but I don't really like this code and I'm sure it can be optimized and look nicer.

require('dotenv').config();
const { readFileSync, writeFileSync } = require('fs');
const { parse, compile }  = require('node-webvtt');
const { translateText } = require('./translate.js');

const inputStr = readFileSync('./subtitles.vtt', 'utf8');
const webVTT = parse(inputStr, { meta: true, strict : true });

const MSTranslateAsync = async (cue) => {
  return translateText(cue, 'fr')
}

const mapAsync = async (vttCues) => {
  return Promise.all(vttCues.map(cue => MSTranslateAsync(cue)))
}
    
mapAsync(webVTT.cues)
  .then(() => {
    outStr = compile(webVTT);
    writeFileSync('./translated_fr.vtt', outStr, 'utf8');
  });

e.g. I'm looking for a way to use only Promises and get your advices to optimize this code

mapAsync(webVTT.cues)
      .then(compile(webVTT))
      .then((data) => writeFileSync('./translated_fr.vtt', data, 'utf8'));
1

There are 1 answers

0
Vladislav Abyshkin On
// not required but I'm using async version of fs in my projects
const fsAsync = require('fs').promises;
//
const compileAndWrite = async ()=>{
    await mapAsync(webVTT.cues);
    let outStr = compile(webVTT); //let outStr = await compile(webVTT);
    await fsAsync.writeFile('./translated_fr.vtt', outStr, 'utf8'); // ou use writeFileSync() 
};

compileAndWrite();