在以下片段中,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')
的輸出被調用。
應該怎樣處理這種情況?
避免['Promise'構造反模式](http://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to -躲開它)! (這不應該阻止你的日誌) – Bergi
也許'getLocation'承諾被拒絕?你不會在任何地方處理錯誤。 – Bergi
無論你做什麼,close事件都會被解僱,readline流並不關心(並等待)你在'line'處理器中做了什麼 – Bergi