Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/460.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在运行第二个之前,请等待函数完成_Javascript_Angular_Ionic Framework - Fatal编程技术网

Javascript 在运行第二个之前,请等待函数完成

Javascript 在运行第二个之前,请等待函数完成,javascript,angular,ionic-framework,Javascript,Angular,Ionic Framework,我的Ionic应用程序中有此功能: clearTransactions() { this.clearIndex = this.clearIndex + 1; if (this.clearIndex < 3) { console.log("click " + (3 - this.clearIndex) + " more times"); } else { this.storage.clearItems()

我的Ionic应用程序中有此功能:

clearTransactions() {
    this.clearIndex = this.clearIndex + 1;
    if (this.clearIndex < 3) {
      console.log("click " + (3 - this.clearIndex) + " more times");
    } else {
      this.storage.clearItems()
      this.storage.getItems().then(
        items => {
          this.items = items
          console.log("test", this.items);
        }
      )
      this.clearIndex = 0;
    }
  }
clearTransactions(){
this.clearIndex=this.clearIndex+1;
如果(此.clearIndex<3){
log(“单击”+(3-this.clearIndex)+“更多次”);
}否则{
this.storage.clearItems()
这是.storage.getItems()。然后(
项目=>{
this.items=项目
console.log(“test”,this.items);
}
)
这个.clearIndex=0;
}
}

出于某种原因,this.storage.getItems()甚至在this.storage.clearItems()完成之前运行。有人能提出解决这个问题的办法吗?谢谢

clearItems返回一个承诺,当它像这样完成时,您已经运行了代码的其余部分:

   this.storage.clearItems().then(()=>{
      // do things after clear
    })

clearItems()是异步的吗?也许你应该为此提供更多的代码,以及为什么你认为它会在GetItems之后运行请不要只发布代码作为答案,还要解释你的代码是做什么的以及它是如何解决问题的。有解释的答案通常更有帮助,质量更好,更容易吸引选票。我不知道为什么这样做有效,但确实有效。谢谢请为将来的读者添加一个解释。@MartinPascual添加了描述