0

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".

SaadRH
  • 140
  • 6

1 Answers1

1

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);
yaron1m
  • 104
  • 5
  • That did not work. The thing is that the console.log(test) is written out before the Console.log(response) in the other class. so it does not wait for the return. – SaadRH Feb 08 '19 at 11:13
  • Can you clarify which of the `console.log` is printing "Promise {pending}", the `test` or the `response`? I suspect that `console.log(response)` is printing out "Promise {pending}" because you write `await response` *after* it. – Richard Li Feb 08 '19 at 16:09
  • console.log(test); is printing "Promise {pending}". Console.log(response); prints out the correct response from the API request. – SaadRH Feb 11 '19 at 11:12