Git Product home page Git Product logo

Comments (5)

rvagg avatar rvagg commented on August 16, 2024

Try 'finish' instead. Also you could try the second callback to the constructor which is like a cleanup at the end.

from through2.

jednano avatar jednano commented on August 16, 2024

Thanks! works!

from through2.

TheLudd avatar TheLudd commented on August 16, 2024

I have a similar problem but neither end nor finish works for me:

  myMongoDbCollection.aggregate([
    ... aggregation pipeline
  ])
    .pipe(through2.obj(function (obj, _, cb) {
      this.push(turnObjIntoString(obj))
      cb()
    }))
    .pipe(process.stdout)
    .on('finish', closeDB)
    .on('error', closeDB)

The result is printed to stdout as expected but the closeDB function is never called and the program never exists.
If I add the listeners before I pipe, then they are called and the program is closed but the print becomes messed up.

from through2.

rvagg avatar rvagg commented on August 16, 2024

@TheLudd you're listening to events on process.stdout, I can't say off the top of my head but it could be that it's not going "close" for you.

  myMongoDbCollection.aggregate([
    ... aggregation pipeline
  ])
    .pipe(through2.obj(function (obj, _, cb) {
      this.push(turnObjIntoString(obj))
      cb()
    }))
    .on('finish', closeDB)
    .on('error', closeDB)
    .pipe(process.stdout)

.. maybe? also, error isn't propagated between pipe() so it's not good enough to just stick it at the end of a chain, you should listen for it on each stream in the chain that could trigger an error.

Also check out:

They might be helpful for various situations.

from through2.

TheLudd avatar TheLudd commented on August 16, 2024

@rvagg Thanks, rearranging the last lines like this did the trick:

    .on('finish', closeDB)
    .on('error', closeDB)
    .pipe(process.stdout)

Will have to read up a bit on piping and errors....

from through2.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.