如何从 Cloud Function Node.js 中的函数访问 Map 的 return 值?
How can I access return value of Map from function in Node.js for Cloud Function?
我读过这个 post How to return values in javascript。但我的问题是,如果我们从异步函数派生它,我们如何获得 Map 值,如下面的代码所示:
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"];
请帮帮我。任何提示和技巧都会很棒。非常感谢您花时间阅读这篇文章post。
两者都正确 Javascript,
- 1- 点 属性 存取器:对象。 属性.
- 2-方括号属性访问:object['property']
- 3- 对象解构:const { 属性 } = object.
此样式被命名为 Object Dot Notation access
const code = response.code;
const token = response.token;
而这个是对象括号符号访问
const code = response["code"];
const token = response["token"];
阅读更多here
我读过这个 post How to return values in javascript。但我的问题是,如果我们从异步函数派生它,我们如何获得 Map 值,如下面的代码所示:
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"];
两者都正确 Javascript,
- 1- 点 属性 存取器:对象。 属性.
- 2-方括号属性访问:object['property']
- 3- 对象解构:const { 属性 } = object.
此样式被命名为 Object Dot Notation access
const code = response.code;
const token = response.token;
而这个是对象括号符号访问
const code = response["code"];
const token = response["token"];
阅读更多here