I have a js function called listClients. It makes a socket.io emit request to grab a list of clients from a node.js server which uses fs to read a file and then send the data back to the client through a callback.
I need to return the callback data to the original function so clients can execute the function and use the data which it returns, but it's not working due to the callback being wrapped in it's own function. What's the best way to work around this?
Client:
function listClients() {
var sender = getCookieData('relay_client_id');
if (sender) {
socket.emit('list_relay_clients', sender, (callback) => {
return callback; //This won't work because it's async
});
return callback; //<---- I need it to be here
}
}
Server:
socket.on('list_relay_clients', function (sender, callback) {
callback(fetchAllClients());
});