Home > Enterprise >  How to reformat output of resolve/promise.all?
How to reformat output of resolve/promise.all?

Time:09-14

I would like help in reformatting the output returned from promise.all, it is returned as an Array of objects but I would like to convert to a string of values only, with the results strung together with one space in between each result.

Current output

[
  { yesnon: 'non' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' }
]

Goal Output: (A one line string with spaces between results, remove "yesnon:")

non yes yes

The code

const fs = require('fs');
const readline = require('readline')


  var mynumtosearch = 56700;
var searchfile = 35;


const p1 = new Promise((resolve, reject) => {
  let lineCount = 0;
  
  let v=0;
let yesnon = "non";

 let readStream = readline.createInterface({
            input: fs.createReadStream('PATH TO MY FILE','utf8')
        });
  
    
  readStream.on("line", (line) => {
      lineCount  ;
     // console.log(line)
if(line==mynumtosearch)
{
yesnon="yes";
console.log("I am here");
v  ;
}
      
    });
    
    readStream.on("close", () =>
      resolve({
       yesnon
      }) 
  
  )
  
});
const p2 = new Promise((resolve, reject) => {

 let readStream = readline.createInterface({
            input: fs.createReadStream('PATH TO MY FILE','utf8')
        });

 let lineCount = 0;  let v=0;
let yesnon = "non";

  readStream.on("line", (line) => {
      lineCount  ;
   //   console.log(line)
if(line==mynumtosearch)
{
yesnon="yes";
console.log("I am here");
v  ;
}
      
    });
  
    readStream.on("close", () =>
      resolve({
   yesnon
      }) )
  
  
});
const p3 = new Promise((resolve, reject) => {
 let lineCount = 0;  let v=0;
let yesnon = "non";

  let readStream = readline.createInterface({
            input: fs.createReadStream('PATH TO MY FILE','utf8')
        });

  readStream.on("line", (line) => {
      lineCount  ;
      //console.log(line)

 if(line==mynumtosearch)
{
yesnon="yes";
console.log("I am here");
v  ;
}     
    });
  

    readStream.on("close", () =>
      resolve({
      yesnon
      }) )
});

console.log(h);
Promise.all([p1, p2, p3]).then((results) => {console.log(results);


});

CodePudding user response:

This would reformat the output array:

let arr = [
  { yesnon: 'non' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' }
];
let output = arr.map(e => e['yesnon']).join(' ');
console.log(output);

CodePudding user response:

This is simple when using operations on collections. The .map() deconstructs ({yesnon}) each element extracting just the value for yesnon (a string) and the join concatenates the resulting strings separated by a space.

results = [
  { yesnon: 'non' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' },
  { yesnon: 'yes' }
]

console.log(results.map( ({yesnon}) => yesnon).join(' '))

CodePudding user response:

Try to use .map() and .join() in your Promise.all().then result

Promise.all([p1, p2, p3]).then((results) => {
  console.log(
    results.map(({ yesnon }) => yesnon).join(' ')
  );
});

at first you convert array of objects to array of string with values, and then you convert this array of strings to string with space between values

  • Related