node.jspromisenode-streamsthrough2

node stream not finish but emit end


I am reading this and wrote my codes based on examples.

My codes:

const through = require('through2'); const bluebird = require('bluebird');

function streamToPromise(stream) {
  return new Promise(function(resolve, reject) {
    stream.resume();
    stream.on("end", resolve);
    stream.on("error", reject);
  });
};

module.exports = {
  up: function up() {
    console.log('startstream');
    // Doc is sequelize Model
    const stream = Doc.createReadStream({
      include: [
        {
          repository: AssociatedRepo,
          as: 'on',
          required: false,
        },
      ],
    });

    stream.pipe(through({ objectMode: true }, (doc, enc, cb) => {
      console.log('docid', doc.id);
      return doc.destroy()
        .then(() => cb());
    }));
    return streamToPromise(stream).then((data) => {
      console.log('xxxxxxxxxx', data);
      return null;
    });
  },
};

I tried to use the above program to read and delete about 400 records. but from log, I found that the stream promise (streamToPromise(stream)) being resolved but still several records remaining. From log, I got:

xxxxxxxxxx undefined
docid 89934915596

How to fix this? Thanks


Solution

  • You'll need to wait for the end of the destination stream of the pipe, currently you are only waiting for the input stream. Use

    const res = stream.pipe(through({objectMode: true}, …));
    return streamToPromise(res);