【发布时间】:2017-08-12 04:06:13
【问题描述】:
在下面的 sn-p 中,lineReader 监听事件 line。当收到line 事件时,它会在Parser 上调用da,这会返回Promise
lineReader.on('line',(line) => {
Parser.da(line).then((info) => {
});
});
lineReader.on('close',() => {
req.end();
});
Parser.da = function(line) {
return new Promise((resolve,reject) => {
geo.getLocation().then((r) => {
console.log(r); return resolve(r);
});
});
}
da 函数返回调用一个同样作用于Promise 的函数。发生的情况是我永远看不到 geo.getLocation 和 readLine.on('close') 的输出被调用。
应该如何处理这种情况?
【问题讨论】:
-
避免使用
Promiseconstructor antipattern! (但这不应该阻止你的日志) -
也许
getLocation承诺被拒绝了?你没有在任何地方处理错误。 -
无论您做什么,都会触发关闭事件,readline 流不关心(并等待)您在
line处理程序中执行的操作
标签: javascript node.js promise