2 var util = require('util')
4 module.exports = function (obj, event, next) {
5 var timeout = setTimeout(gotTimeout, 10)
6 obj.once(event, gotResult)
8 function gotTimeout () {
9 obj.removeListener(event, gotResult)
10 next(new Error('Timeout listening for ' + event))
13 function gotResult () {
14 result = Array.prototype.slice.call(arguments)
16 timeout = setTimeout(gotNoMoreResults, 10)
17 obj.once(event, gotTooManyResults)
19 function gotNoMoreResults () {
20 obj.removeListener(event, gotTooManyResults)
21 var args = [null].concat(result)
22 next.apply(null, args)
24 function gotTooManyResults () {
25 var secondResult = Array.prototype.slice.call(arguments)
27 next(new Error('Got too many results, first ' + util.inspect(result) + ' and then ' + util.inspect(secondResult)))