0

I would like to return result from response.on in the signIn function.

const signIn = async (password) => {
  var request = new DeviceAuthQuery();
  request.setPassword(password);
  var response = client.authenticate(request, {}, (err, response) => {
      console.log("Request Sent 1111");
  });

  response.on('data', function(reply) {
      console.log("Data Received 2222");
      console.log('Data: 22222'+ reply.getAuthjwt());
      var result = reply.getAuthjwt()
      return result
  });

  return result;  
}

export {
    signIn
};
Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
  • As you defined your function as `async` you probably want to actually `await` something inside of it that is async too, e.g. `client.authenticate()`. Also an async function returns a `Promise` that resolves with the return value. – trixn Feb 20 '20 at 16:21

1 Answers1

-1

Your result variable is out of scope.

Define var result; outside of your response.on then set result = reply.getAuthjwt().

Otherwise, I believe that you should be able to return response.on and just return reply.getAuthjwt() within your function.

const signIn = async (password) => {
  var request = new DeviceAuthQuery();
  request.setPassword(password);
  var response = client.authenticate(request, {}, (err, response) => {
      console.log("Request Sent 1111");
  });
  return response.on('data', function(reply) {
      console.log("Data Received 2222");
      console.log('Data: 22222'+ reply.getAuthjwt());
      return reply.getAuthjwt()
  });
}

export {
    signIn
};

or

const signIn = async (password) => {
  var result;
  var request = new DeviceAuthQuery();
  request.setPassword(password);
  var response = client.authenticate(request, {}, (err, response) => {
      console.log("Request Sent 1111");
  });
  response.on('data', function(reply) {
      console.log("Data Received 2222");
      console.log('Data: 22222'+ reply.getAuthjwt());
      result = reply.getAuthjwt()
  });
  return result;
}

export {
    signIn
};

I also wonder if you can use

const signIn = async (password) => {
  var result;
  var request = new DeviceAuthQuery();
  request.setPassword(password);
  client.authenticate(request, {}, (err, response) => {
      response.on('data', function(reply) {
        console.log("Data Received 2222");
        console.log('Data: 22222'+ reply.getAuthjwt());
        result = reply.getAuthjwt()
     });
  });

  return result;  
}

export {
    signIn
};
Brandon
  • 1,447
  • 2
  • 21
  • 41
  • `response.on` is asynchronous, runs *multiple times*, and don't use promises. None of your attempts will work. – Quentin Feb 20 '20 at 16:38
  • I tried all the methods but the only one that is functional is the first proposition. But it returns a response object: U {a: P, h: ƒ, j: ƒ, c: ƒ, b: ƒ, …} not the reply.getAuthjwt () result Data: 22222eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c – Borhen Ftiss Feb 20 '20 at 16:42