0

I am studying through2 and sequelize.

My codes:

  return Doc.createReadStream({
    where: { /*...*/ },
    include: [
      {
        /*...*/
      },
    ],
  })
  .pipe(through({ objectMode: true }, (doc, enc, cb) => {
    Comment.findOne(null, { where: { onId: doc.id } }).then((com) => { /* sequelize: findOne*/
      com.destroy(); /* sequelize instance destroy: http://docs.sequelizejs.com/manual/tutorial/instances.html#destroying-deleting-persistent-instances */
      cb();
    });
  }))
  .on('finish', () => {
    console.log('FINISHED');
  })
  .on('error', err => console.log('ERR', err));

I am trying to express my question clearly. Doc and Comment are sequelize Models. I want to use stream to read Doc instances from database one by one and delete comments on each Doc instance. Comment.findOne and com.destroy() will both return promises. I want to the promises resolved for each doc and then call cb(). But my above codes cannot work, before com be destroyed, the codes already finish running.

How to fix it? Thanks

I wrap the above piece of codes in mocha test, like

it('should be found by readstream', function _testStream(){
  /* wrap the first piece of codes here*/
});

But before stream finished reading, the test exist.

Rob
  • 14,746
  • 28
  • 47
  • 65
BAE
  • 8,550
  • 22
  • 88
  • 171

1 Answers1

0

You can wait for another promise by returning the promise and using another .then.

You may need to check for the com result being null as well, before running .destroy().

  .pipe(through({ objectMode: true }, (doc, enc, cb) => {
    Comment.findOne(null, { where: { onId: doc.id } })
      .then(com => com.destroy())
      .then(()=> cb())
      .catch(cb)
  }))

Then when running the test in mocha, you need to wait for the asynchronous stream by adding done to the test function signature and calling done() on completion or error.

it('should be found by readstream', function _testStream(done){
  ...
  .on('finish', () => done())
  .on('error', done)
})
Matt
  • 68,711
  • 7
  • 155
  • 158
  • Great. but for now, I cannot use `await` – BAE Dec 01 '17 at 03:53
  • I tried the first solution, but again, before stream finished reading, the test existed. I think the problem is that the whole promise chain is async.. – BAE Dec 01 '17 at 04:02
  • Ah yes. You need to use `done` with mocha and streams to signify the end of the async test... I'll add that in – Matt Dec 01 '17 at 04:33