'How to make JavaScript execution wait for socket.on function response?

var data = bar();

function bar() {
  var result;

  socket.emit('message', 'test');
  socket.on('msg', function(message) {
    result = message;
  });

  return result;
}

Here, the result value is getting undefined. How to assign the value retrieved from the socket.on function to result variable and return?

This is my other end that receives the socket.io message event.

var io = require('socket.io').listen(server);

io.sockets.on('connection', function(socket, username) {
  socket.on('message', function(message) {
    var result = 'test value'
    socket.emit('msg', result);
  });
});

Trying to return a object in acknowlegment.

io.sockets.on('connection', function (socket) {
    socket.on('message', function(message, ackCallback) {
        console.log("server received message", message);
        var result = tools.execute(message);  
        console.log(typeof(result)); // result is returned a object
        ackCallback(result);    
    });  
});

P.S. I tried to pass a simple object

var result = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
ackCallback(result);

The result value is getting passed for this object.


tools.execute(message); is returning this object. Which is not getting passed through the ackCallback.

{ uuid: '155C75EA-CB23-4172-85EB-3E256A271D8D', name: '', type: 'Object3D', parent: null, children: [ { uuid: 'D778A19B-F935-4B06-8536-54494BC5F920', name: '', type: 'Mesh', parent: [Circular], children: [], up: [Object], position: [Object], rotation: [Object], quaternion: [Object], scale: [Object], matrix: [Object], matrixWorld: [Object], matrixAutoUpdate: true, matrixWorldNeedsUpdate: false, layers: [Object], visible: true, castShadow: false, receiveShadow: false, frustumCulled: true, renderOrder: 0, userData: {}, geometry: [Object], material: [Object], drawMode: 0 }, { uuid: '9FD1CACF-6A2B-4932-8FA2-B0A4AF618F8D', name: '', type: 'Mesh', parent: [Circular], children: [], up: [Object], position: [Object], rotation: [Object], quaternion: [Object], scale: [Object], matrix: [Object], matrixWorld: [Object], matrixAutoUpdate: true, matrixWorldNeedsUpdate: false, layers: [Object], visible: true, castShadow: true, receiveShadow: false, frustumCulled: true, renderOrder: 0, userData: {}, geometry: [Object], material: [Object], drawMode: 0 } ], up: { x: 0, y: 1, z: 0 }, position: { x: 0, y: 0, z: 0 }, rotation: { _x: 0, _y: 0, _z: 0, _order: 'XYZ', onChangeCallback: [Function: onRotationChange] }, quaternion: { _x: 0, _y: 0, _z: 0, _w: 1, onChangeCallback: [Function: onQuaternionChange] }, scale: { x: 1, y: 1, z: 1 }, matrix: { elements: Float32Array [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] }, matrixWorld: { elements: Float32Array [ 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1 ] }, matrixAutoUpdate: true, matrixWorldNeedsUpdate: false, layers: { mask: 1 }, visible: true, castShadow: false, receiveShadow: false, frustumCulled: true, renderOrder: 0, userData: {} }



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source