reactjs - 如何将响应值返回给调用者函数
问题描述
我正在调用另一个类中的一个函数,其中包含一个 API 请求。我想将 API 请求的响应返回给我调用函数的类。但是 Console.log 会写出“Promise {pending}”。
let test: Object = Utility.getManager(this.props.graphClient);
console.log(test);
在这里,我使用参数调用“实用程序”类中的函数“getManager”。
public static async getManager(Client: MSGraphClient): Promise<Object> {
return await Client
.api("/me/manager")
.version("v1.0")
.get(async (error, response: any, rawResponse?: any): Promise<any> => {
// handle the response
console.log(response);
return await response;
});
}
在这里,我尝试将 API 请求的响应发送回以保存在“测试”中。
解决方案
getManager
是一个异步函数,当你调用它时,你会得到一个承诺(就像所有异步函数一样)。
如果要记录结果,您必须:
let test: Object = await Utility.getManager(this.props.graphClient);
console.log(test);
推荐阅读
- python - Python ZeroMQ PUSH/PULL 逻辑,将高水位标记设置为低端 puller 而不会丢失任何消息
- java - 从文件中获取值并将它们分配给java中的数组
- c# - 需要帮助将事件按钮与文件连接起来
- c++ - 为什么用户交互会阻止qt本地事件循环中的http请求
- python - 并行化一个简单的计算
- c++ - 有人可以帮我弄清楚如何从我的代码中删除来自hackerrank的分段错误
- ios - 在 Objective-C 中使用指纹或 Face ID 进行应用内购买
- python - 在 django 中可以使用 pandas 吗?
- c - 调试器在 Visual Studio Code 中无法正常工作,它说启动:程序“不存在和 2 个其他问题
- python - 更改 x 轴比例 im matplotlib imshow?