Last active
June 3, 2021 18:55
-
-
Save hbobenicio/adbe1313a22a693679954bf7a71cfe5b to your computer and use it in GitHub Desktop.
async generator example with node.js stream pipelines
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const path = require('path'); | |
const { pipeline } = require('stream/promises'); | |
async function generateFixtures() { | |
await Promise.all([ | |
generateFooFixture(), | |
generateBarFixture(), | |
generateCazFixture(), | |
//generateDazFixture(), | |
]); | |
} | |
async function generateFooFixture() { | |
const writer = fs.createWriteStream(config.fixtures.foo.filepath); | |
writer.write(['name', 'email', 'age'].join(config.csv.delimiter) + '\n'); | |
await pipeline( | |
async function*() { | |
for (let i = 1; i <= config.fixtures.foo.totalRecords; i++) { | |
const foo = { | |
name: `foo-${i}`, | |
email: `${i}@foo.com`, | |
age: `${i}`, | |
}; | |
const line = [foo.name, foo.email, foo.age].join(config.csv.delimiter); | |
yield line + '\n'; | |
} | |
}, | |
writer, | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment