zemirco/json2csv

What's the right way to use parser.promise() ?

VincentClair opened this issue · 2 comments

Hello, I have a problem to use parser.promise(). Am is using wrong ?

json2csv: 5.0.6
node: 12.0.17

If I have the following code, not all records are exported to csv :

    const parser = new AsyncParser(
        { header: true, withBOM: true },
        { objectMode: true }
    );
 
    for await (const record of iterable) {
        parser.input.push(record);
    }
    // no more records, close asyncParser
    parser.input.push(null);

    // cast bad type: if promise argument true, value would not be "undefined"
    return parser.promise(true) as Promise<string>;

After a rework, it seems to work :

    const parser = new AsyncParser(
        { header: true, withBOM: true },
        { objectMode: true }
    );
    const p = parser.promise();
 
    for await (const record of iterable) {
        parser.input.push(record);
    }
    // no more records, close asyncParser
    parser.input.push(null);

    return p as Promise<string>;

I would like to be sure that's the right way and parser.promise() must be declared before using parser.input ?

That's right.

promise should be called before pushing the data.
The reason for this is that promise keeps all the data in memory from the time that you call it so it can return the whole object.

If you call it at the end, the data has already been processed but not kept, as it's expected for stream processing pipelines.

Thanks for your quick answer :)