Skip to content

Instantly share code, notes, and snippets.

@reggi
Last active August 30, 2019 16:42
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save reggi/923c6704104dd50395e5 to your computer and use it in GitHub Desktop.
Save reggi/923c6704104dd50395e5 to your computer and use it in GitHub Desktop.
Socket.io emit async
var io = require('socket.io-client')("http://localhost:3001")
var Promise = require("bluebird")
io.emitAsync = Promise.promisify(io.emit)
io.emitAsync("report", {
"name": "thomas"
}).then(function(data){
console.log(data)
}).catch(function(e){
console.log(e.message)
})
// this works if you return a truthy first-parameter callback("my error", data)
var _ = require("underscore")
var io = require('socket.io-client')("http://localhost:3001")
var Promise = require("bluebird")
io.emitAsync = function(event, payload){
return new Promise(function (resolve, reject) {
return io.emit(event, payload, function(){
var args = _.toArray(arguments)
if(args[0]) return reject(new Error(args[0]))
return resolve.apply(null, args)
})
})
}
io.emitAsync("report", {
"name": "thomas"
}).then(function(data){
console.log(data)
}).catch(function(e){
console.log(e.message)
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment