SaadRH
SaadRH

Reputation: 150

How to return the response value to the caller function

I am calling a function in another class, that contains a API request. I want to return the response from the API request back to the class where i called the function from. But the Console.log writes out "Promise {pending}".

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

Here i call the function "getManager" in class "Utility" with a parameter.

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

Here i try to send back the response from the API request to be saved in "test".

Upvotes: 0

Views: 425

Answers (1)

yaron1m
yaron1m

Reputation: 104

getManager is an async function, and when you call it you get a promise (like all async functions).

If you want to log the result you must:

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

Upvotes: 1

Related Questions