« get me outta code hell

waitForData.js « util - tui-lib - Pure Node.js library for making visual command-line programs (ala vim, ncdu)
about summary refs log tree commit diff
path: root/util/waitForData.js
blob: 75f740eff245f486cb2ee65c0c26c47080ffb2ca (plain)
1
2
3
4
5
6
7
8
9
10
11
export default function waitForData(stream, cond = null) {
  return new Promise(resolve => {
    const listener = data => {
      if (cond ? cond(data) : true) {
        resolve(data)
        stream.removeListener('data', listener)
      }
    }
    stream.on('data', listener)
  })
}