Skip to content

Commit

Permalink
feat: Improve async promise to optionally not return (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
juanjoDiaz authored and knownasilya committed Sep 9, 2019
1 parent f99408c commit 3e296f6
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
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());
});


};

0 comments on commit 3e296f6

Please sign in to comment.