-
-
Save roninhack/1b65bcb7647dc07fbfb3b1e4a6bcecd8 to your computer and use it in GitHub Desktop.
Socket.io emit async
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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