Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve async promise to optionally not return #421

Merged
merged 1 commit into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ asyncParser.input.push(null); // Sending `null` to a stream signal that no more
* `fromInput` allows you to set the input stream.
* `throughTransform` allows you to add transforms to the stream.
* `toOutput` allows you to set the output stream.
* `promise` returns a promise that resolves when the stream ends or errors.
* `promise` returns a promise that resolves when the stream ends or errors. Takes a boolean parameter to indicate if the resulting CSV should be kept in-memory and be resolved by the promise.

```js
const { createReadStream, createWriteStream } = require('fs');
Expand All @@ -279,12 +279,23 @@ const fields = ['field1', 'field2', 'field3'];
const opts = { fields };
const transformOpts = { highWaterMark: 8192 };

// Using the promise API
const input = createReadStream(inputPath, { encoding: 'utf8' });
const output = createWriteStream(outputPath, { encoding: 'utf8' });
const asyncParser = new JSON2CSVAsyncParser(opts, transformOpts);
asyncParser.fromInput(input).toOutput(output).promise()
const parsingProcessor = asyncParser.fromInput(input);

parsingProcessor.promise()
.then(csv => console.log(csv))
.catch(err => console.error(err));;
.catch(err => console.error(err));

// Using the promise API just to know when the process finnish
// but not actually load the CSV in memory
const input = createReadStream(inputPath, { encoding: 'utf8' });
const output = createWriteStream(outputPath, { encoding: 'utf8' });
const asyncParser = new JSON2CSVAsyncParser(opts, transformOpts);
const parsingProcessor = asyncParser.fromInput(input).toOutput(output);

parsingProcessor.promise(false).catch(err => console.error(err));
```

you can also use the convenience method `parseAsync` which accept both JSON arrays/objects and readable streams and returns a promise.
Expand Down
9 changes: 8 additions & 1 deletion lib/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ class JSON2CSVAsyncParser {
return this;
}

promise() {
promise(returnCSV = true) {
return new Promise((resolve, reject) => {
if (!returnCSV) {
this.processor
.on('finish', () => resolve())
.on('error', err => reject(err));
return;
}

let csvBuffer = [];
this.processor
.on('data', chunk => csvBuffer.push(chunk.toString()))
Expand Down
30 changes: 30 additions & 0 deletions test/JSON2CSVAsyncParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -955,4 +955,34 @@ module.exports = (testRunner, jsonFixtures, csvFixtures, inMemoryJsonFixtures) =
t.end();
});

testRunner.add('should not return if ret option is set to false', (t) => {
const parser = new AsyncParser();
const configuredParser = parser.fromInput(jsonFixtures.default())
let csv = '';

configuredParser.processor.on('data', chunk => (csv += chunk.toString()));

configuredParser.promise(false)
.then(res => {
t.equal(res, undefined);
t.equal(csv, csvFixtures.defaultStream);
})
.catch(err => t.notOk(true, err.message))
.then(() => t.end());
});

testRunner.add('should catch errors even if ret option is set to false', (t) => {
const opts = {
fields: ['carModel', 'price', 'color', 'transmission']
};

const parser = new AsyncParser(opts);

parser.fromInput(jsonFixtures.defaultInvalid()).promise(false)
.then(() => t.notOk(true))
.catch(err => t.ok(err.message.includes('Invalid JSON')))
.then(() => t.end());
});


};