Chris Thompson
Chris Thompson

Reputation: 35598

End NodeJS program using streams

I have a small program that is running an ETL pipeline on some XML data. I'm using pump (package) to combine a Readable, Duplex, and Writeable streams. I'm getting the final 'done' callback, but the program doesn't exit. Is the only way to handle this to call process.exit(0)?

Here's the code snippet:

pump(
    fileStream,
    parser,
    inserter,
    (err) => {
        if (err) {
            console.log(err);
        } else {
            console.log('done');
        }
    });

Edit: Just to close the loop, what @FakeFootball suggested does the trick:

pump(
        fileStream,
        parser,
        inserter,
        (err) => {
            if (err) {
                console.log(err);
            } else {
                parser.destroy();
                inserter.destroy();
                fileStream.destroy();
            }
        });

Upvotes: 3

Views: 278

Answers (1)

FakeFootball
FakeFootball

Reputation: 477

Without seeing the rest of your code, my guess is you aren't closing/destroying any of your streams so they are still active. Node won't close as it is still listening for potential actions on those streams. See the example in the usage section of the pump documentation for how they close the stream and it destroys the rest of them. Pump usage

Upvotes: 2

Related Questions