D.B
D.B

Reputation: 4289

then() block of promise does not wait for internal await

I have the following methods:

  this.skickPlayerService.loadSkick().then(
  (skick) => {
    var skickBlob = skick as Blob;
  },
  (error) => {}
);

The async method calls looks as follow:

async loadSkick(): Promise<any> {
try {
  return await this.graphClient
    .api('/me/drive/items/25647924903216B8%21227697')
    .get(async (err, res) => {
      if (err) {
        return;
      }
      return await fetch(res['@microsoft.graph.downloadUrl']).then(
        async function (response) {
          return await response.blob(); // I need the call to wait until this is executed
        }
      );
    });
  } catch (error) {}
}

The problem is that loadSkick() returns when .then is executed, but the value is still null because the next internal call "return await response.blob();" hasn't been executed yet.

I need to return to the caller only once the result of the return await response.blob(); is executed

Upvotes: 0

Views: 242

Answers (1)

jfriend00
jfriend00

Reputation: 707328

From the doc, it looks like this should work:

async loadSkick(): Promise<any> {
    const res = await this.graphClient
        .api('/me/drive/items/25647924903216B8%21227697')
        .get();
    const response = await fetch(res['@microsoft.graph.downloadUrl']);
    return response.blob();
}

The caller to loadSkick() will have to use the returned promise with either await or .then() to get the final result.

Upvotes: 1

Related Questions