Skip to content

Instantly share code, notes, and snippets.

@tomtobac
Created September 21, 2016 20:47
Show Gist options
  • Save tomtobac/77f863190a06763c3ce083d1c89b68ab to your computer and use it in GitHub Desktop.
Save tomtobac/77f863190a06763c3ce083d1c89b68ab to your computer and use it in GitHub Desktop.
'use strict'
// const Promise = require('es6-promise').Promise;
const csv = require('csv-parse')
const fast = require('fast-csv');
const fs = require('fs')
const files = ['csv_1.csv', 'csv_2.csv']
const promises = []
files.forEach(file => {
let promise = new Promise((resolve, reject) => {
const data = []
fs.createReadStream(`./csv/${file}`)
.pipe(csv())
.on('data', (record) => data.push(record))
.on('error', (err) => reject(err))
.on('finish', () => {
resolve(data)
})
})
promises.push(promise)
});
Promise.all(promises)
.then(values => {
let t = []
values.forEach((value, i) => {
if (i != 0) value.shift()
t = t.concat(value)
})
// console.log(t)
let ws = fs.createWriteStream('./csv/all.csv')
fast.writeToStream(ws, t, {headers: false})
// fast.writeToString(t, {headers: true}, (err, data) => console.log(data))
})
.catch(err => console.log(err));
// p.then(
// (value) => console.log(`good: ${value}`),
// (error) => console.log(`error: ${error}`)
// )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment