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); }
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"];
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