Skip to content

Instantly share code, notes, and snippets.

@hbobenicio
Last active June 3, 2021 18:55
Show Gist options
  • Save hbobenicio/adbe1313a22a693679954bf7a71cfe5b to your computer and use it in GitHub Desktop.
Save hbobenicio/adbe1313a22a693679954bf7a71cfe5b to your computer and use it in GitHub Desktop.
async generator example with node.js stream pipelines
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