1
我不能完全找到一個很好的方法來做到這一點,但這裏是我的基本方法。其目標是觀察對象的狀態,直到找到與正則表達式匹配的消息。使用承諾觀察光標並停止光標
Cylon.waitForMessage(regex) {
check(regex, RegExp);
const observer = this.connections.find({
name: 'messages'
}).observe({
changed: function (doc) {
console.log(doc.lastMessage) // this is the last message to scan.
}
});
let promise = new Promise((resolve, reject) => {
// need to find the last message here and check if it matches regex
}).then(() => observer.stop());
return Promise.await(promise);
}
什麼是適當的方法來做到這一點?承諾完成後,你如何阻止聽衆或觀察者?
爲什麼不直接從'changed'函數內部停止觀察員?測試該函數的消息,如果通過則停止觀察者。 – MinusFour
因爲我需要它在繼續下一步之前等待消息,否則它會嘗試一次發送太多的命令。 – corvid