title: Adding a timeout for a callback within a promise and rejecting
body: <p>I'm converting socket.io's emit function to a promise like this.</p>
  
  <pre><code>var io = require("socket.io-client")
  var Promise = require("bluebird")
  var _ = require("underscore")
  
  io.emitAsync = function(event, payload){
    return new Promise(function (resolve, reject) {
      return io.emit(event, payload, function(){
        var args = _.toArray(arguments)
        if(args[0]) reject(new Error(args[0]))
        resolve.apply(null, args)
      })
    })
  }
  </code></pre>
  
  <p>I couldn't use <code>promiseify</code> because the <code>io.emit</code> callback doesn't return a node-style callback signature. <a href="https://stackoverflow.com/a/20916142/340688">The first argument can't be an error object.</a> So the plan is to pass a string error message, the promise above will reject if the first argument is <code>truthy</code>. This is all fine and dandy, here's the problem. </p>
  
  <p>If you don't return a <code>callback</code> in your <code>socket.on</code> then this promise above will never <code>resolve</code> or <code>reject</code> anything. So what I need is a timeout this <code>callback</code>, if nothing comes back within an allotted period of time then it will <code>reject(new Error("timeout"))</code>. Thing is I have no clue how to do that. Ideally I can set the <code>seconds</code> to wait within the function above, or if know there's no callback for a given event I'd want to <code>resolve</code>, so ideally it's a parameter of <code>emitAsync</code> itself. Thoughts?</p>