Skip to content
Advertisement

How can I access return value of Map from function in Node.js for Cloud Function?

I had read this post How to return values in javascript. But my question is how do we get the Map value if we derived it from asynchronous function like in this code below:

async function exampleToken(profile) {
  let response;
  const tkAdmin = {
    admin: true,
  };
  const tkInvestors = {
    investor: true,
  };
  if (profile == 1) {
    response = {
      "token": tkAdmin,
      "code": 1,
    };
  } else if (profile == 2) {
    response = {
      "token": tkInvestors,
      "code": 2,
    };
  }
  return Promise.resolve(response);
}
I want to use the value from this function using this code:

const response = await exampleToken(profile);
// Is this correct:
const code = response.code;
const token = response.token;
// or
const code = response["code"];
const token = response["token"];
Please, help me out. Any tips and trick will be great. Thank you very much for spending time to read this post.

Advertisement

Answer

Both are correct in Javascript,

  • 1- Dot property accessor: object. property.
  • 2- Square brackets property access: object[‘property’]
  • 3- Object destructuring: const { property } = object.

This style is named Object Dot Notation access

const code = response.code;
const token = response.token;

and this one is Object Bracket notation access

const code = response["code"];
const token = response["token"];

Read more here

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement