Ionic 等待功能完成后再运行第二个

我的 Ionic App 中有这个功能:


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;

    }

  }

出于某种原因,this.storage.getItems() 甚至在 this.storage.clearItems() 完成之前运行。谁能为这个问题提出解决方案?谢谢!


一只甜甜圈
浏览 82回答 1
1回答

偶然的你

clearItems返回一个 promise,当它像这样完成时,您已经运行了剩余的代码:&nbsp; &nbsp;this.storage.clearItems().then(()=>{&nbsp; &nbsp; &nbsp; // do things after clear&nbsp; &nbsp; })
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript