title: Read csv asynchronous - NodeJS
body: <p>I'm looping an array and reading from a CSV file so I can reduce its data, I've implemented something like this. I want to have a Map variable populated from a csv file in memory and get data from it. But I want to populate it once (or when I explicitly do so, but that's other thing).</p>
  <p>when running I would like to print like this:</p>
  <pre><code>before 
  after
  </code></pre>
  <p>But I'm getting this:</p>
  <pre><code>before
  before
  before
  after
  after
  after
  </code></pre>
  <p>Apparently when reading from the CSV is not awaiting. Is there any way I can read from CSV asynchronous?</p>
  <p>This is my code:</p>
  <pre><code>let myMap = new Map();
  const parseMap = async(stream) =&gt; {
      const map = new Map();
      return new Promise((resolve, reject) =&gt; {
          stream
             .on('data', elems =&gt; {
                const key = elems[0];
                const value = elems.slice(-1)[0];
                map.set(key, value);
             })
             .on('error', () =&gt; reject)
             .on('end', () =&gt; {
               console.log('promise');
               resolve(map);
          });
    });  
  }
  
  const getMap = async(path_string) =&gt; {
      const stream = fs.createReadStream(path_string).pipe(parse({delimiter: DELIMITER}));
      return await parseMap(stream);
      
  }
  const translate = async (key) =&gt; {
      if (!myMap.size) {
          console.log('before');
          myMap = await getMap('my-csv.csv');
          console.log('after');
      }
      return myMap.get(key);
  };
  
  const guidRankings = await ['a', 'b', 'c'].reduce(async (accumulator, value, idx) =&gt; {
          const data = await translate(value);
          if (data) {
              (await accumulator).push({
                  data,
                  order: idx
              });
          }
          return accumulator;
      }, Promise.resolve([]));
  </code></pre>