0

I already asked this question regarding SPFx extensions.

But now I have the same problem I had in that question, but for a SPFx Web part. I use the exact same _getUserInformation() method as mentioned in the answer and which worked for my extension.

private _getUserInformation() : Promise<any> {
  const res = this.context.msGraphClientFactory
    .getClient()
    .then((client: MSGraphClient): void => {
    client
      .api('/me')
      .select("department,displayName,companyName,officeLocation")
      .get((error, response: MicrosoftGraph.User, rawResponse?: any) => {
        // do something
      });
    });

  return Promise.resolve<any>(res);
}

And I call it like that:

public async render(): Promise<void> {
  await this._getUserInformation();
  this._doOtherStuff();
}

But _doOtherStuff() gets called before the the _getUserInformation()-method is finished. I know it seems to be the same question as in my other issue, and probably it is, but why is it like that? I really used the exact same code, but this time in a web part, not an extension.

0

The problem here is that you are returning a resolved promise, it basically tells the caller "hey I'm done", which in your case might or might not be true depending on the timing of things.
I suggest you return res right away without wrapping it in a returned promise. Or you could switch to the async/await syntax for this method as well to be consistent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.