« get me outta code hell

tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--util/waitForData.js6
1 files changed, 4 insertions, 2 deletions
diff --git a/util/waitForData.js b/util/waitForData.js
index bf40c52..ed88402 100644
--- a/util/waitForData.js
+++ b/util/waitForData.js
@@ -1,9 +1,11 @@
 module.exports = function waitForData(stream, cond = null) {
   return new Promise(resolve => {
-    stream.on('data', data => {
+    const listener = data => {
       if (cond ? cond(data) : true) {
         resolve(data)
+        stream.removeListener('data', listener)
       }
-    })
+    }
+    stream.on('data', listener)
   })
 }