【问题标题】:then() block of promise does not wait for internal awaitthen() 承诺块不等待内部等待
【发布时间】:2020-08-07 13:22:29
【问题描述】:

我有以下方法:

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

异步方法调用如下:

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

问题是loadSkick()在.then执行的时候返回,但是值还是null因为下一个内部调用“return await response.blob();”尚未执行。

我只需要返回调用者一次的结果返回 await response.blob();被执行

【问题讨论】:

标签: angular promise


【解决方案1】:

从文档看来,这应该可以工作:

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

loadSkick() 的调用者必须将返回的 Promise 与 await.then() 一起使用才能获得最终结果。

【讨论】:

  • 你太棒了。谢谢。
猜你喜欢
  • 2015-08-15
  • 2019-12-10
  • 1970-01-01
  • 2018-12-24
  • 2019-04-29
  • 2021-10-14
  • 2013-09-16
  • 2018-02-03
  • 2018-03-05
相关资源
最近更新 更多