首页 > 解决方案 > 如何将响应值返回给调用者函数

问题描述

我正在调用另一个类中的一个函数,其中包含一个 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 请求的响应发送回以保存在“测试”中。

标签: reactjstypescriptsharepointspfx

解决方案


getManager是一个异步函数,当你调用它时,你会得到一个承诺(就像所有异步函数一样)。

如果要记录结果,您必须:

let test: Object = await Utility.getManager(this.props.graphClient);
console.log(test);

推荐阅读