Error: Future resolved more than once


#1

We have an application that needs to make a socket call to a server, sending over a JSON request and receiving a JSON response that is marked as complete with a newline character. I did not write the interface of the remote server, nor do I have any ability to change it. So we are using the following function to send the request and obtain the result which can many times fill the buffer multiple times.

function sendCommandAsync(socket, aCommand, callback) {
    let sendResult = '';
    try {
      socket.write(aCommand);
      socket.on('data', (data) => {
        const newData = data.toString();
        sendResult += newData;
        if (newData.indexOf('\n') !== -1) {
          sendResult = sendResult.slice(0, -1);
          callback(null, sendResult);
        }
      });
    } catch (error) {
      throw error;
    }
  }

This function is the wrapped using Meteor.wrapAsync(sendCommandAsync) and called passing in the socket and command. The results returned are CORRECT and we can use the data. However, we get an error logged to the console for every request:

I20161201-07:59:10.370(-6)? Exception in callback of async function: Error: Future resolved more than once
I20161201-07:59:10.776(-6)?     at Object.Future.return (C:\Users\xxx\AppData\Local\.meteor\packages\meteor-tool\1.4.2_3\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\fibers\future.js:261:10)
I20161201-07:59:10.777(-6)?     at Object.<anonymous> (C:\Users\xxx\AppData\Local\.meteor\packages\meteor-tool\1.4.2_3\mt-os.windows.x86_32\dev_bundle\server-lib\node_modules\fibers\future.js:349:16)
I20161201-07:59:10.777(-6)?     at runWithEnvironment (packages\meteor.js:1176:24)

I cannot figure out how to correct this or where it may be coming from since it is “working” from the applications point of view. Any thoughts?