title: How to make JavaScript execution wait for socket.on function response?
body: <pre><code>var data = bar();
  
  function bar() {
    var result;
  
    socket.emit('message', 'test');
    socket.on('msg', function(message) {
      result = message;
    });
  
    return result;
  }
  </code></pre>
  
  <p>Here, the result value is getting <code>undefined</code>. How to assign the value retrieved from the <code>socket.on</code> function to <code>result</code> variable and return?</p>
  
  <p>This is my other end that receives the <code>socket.io</code> message event.</p>
  
  <pre><code>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);
    });
  });
  </code></pre>
  
  <hr>
  
  <p><strong>Trying to return a object in acknowlegment.</strong></p>
  
  <pre><code>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);    
      });  
  });
  </code></pre>
  
  <p>P.S. I tried to pass a simple object </p>
  
  <pre><code>var result = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
  ackCallback(result);
  </code></pre>
  
  <p>The result value is getting passed for this object.</p>
  
  <hr>
  
  <p>tools.execute(message); is returning this object. Which is not getting passed through the ackCallback. </p>
  
  <p>{ 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: {} }</p>