我的代码:
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));
我试图清楚地表达我的问题。Doc
并且Comment
是续集模型。我想使用流从数据库中一一读取文档实例并删除每个文档实例上的注释。Comment.findOne
并且com.destroy()
都会返回承诺。我想为每个解决的承诺doc
然后调用cb()
. 但是我上面的代码不能工作,在com
被销毁之前,代码已经运行完毕。
如何解决?谢谢
我将上面的代码包装在mocha
测试中,比如
it('should be found by readstream', function _testStream(){
/* wrap the first piece of codes here*/
});
但在流完成读取之前,测试存在。