简体   繁体   English

如何删除 setTimeout 并在 angular 中使用异步等待方法

[英]How to remove the setTimeout and use async await method in angular

If I removed the setTimeout form my below mentioned code then the blocks of code (getUnderlyingData) is running before the previous response.如果我从下面提到的代码中删除了setTimeout那么代码块 (getUnderlyingData) 将在上一个响应之前运行。 So I am not getting the proper response.所以我没有得到正确的回应。 Please anyone help me to resove this issue.请任何人帮助我解决这个问题。 Current code:当前代码:

await this.TableauService.initViz(this.TableauViz, this.DataContainer, this.DataURL).then(([uniqData, TableauViz]) => {
    this.VizWorkbook = TableauViz;
    setTimeout(() => {
      this.SheetData = this.TableauService.getUnderlyingData(TableauViz, "KPI",(response) => {
        console.log("sheet data",response);
      }); 
    },7000);
  });
}

If you use await you don't need to use then to resolve the promise, await will do that for you.如果您使用await您不需要使用then来解决承诺, await将为您完成。

An async function can contain an await expression that pauses the execution of the async function and waits for the passed promise's resolution after which it resumes the async function's execution and returns the resolved value.异步函数可以包含一个await表达式,该表达式暂停异步函数的执行并等待传递的承诺的解析,之后它会继续异步函数的执行并返回解析的值。

async functionName(){
let result = await this.TableauService.initViz(this.TableauViz, this.DataContainer, this.DataURL)
}

The above result variable holds your resolved value with that you can do whatever you want.上面的result变量保存您的解析值,您可以随心所欲。

Or you can simply add this line this.VizWorkbook = TableauViz;或者您可以简单地添加这一行this.VizWorkbook = TableauViz; inside your getUnderlyingData(.. { this.VizWorkbook = TableauViz; ... }在你的getUnderlyingData(.. { this.VizWorkbook = TableauViz; ... }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM