首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >带有then的VueJS函数

带有then的VueJS函数
EN

Stack Overflow用户
提问于 2018-07-25 03:20:56
回答 2查看 5.5K关注 0票数 0

我有以下方法:

代码语言:javascript
复制
    getData(id){
          this.$store.dispatch('getData', {
            employeeId: this.employeeId
          }).then(() => {
            if (this.employeeData.length) {
              // here some code...
            }
          });
        },
 selectEmployee(d) {

      this.getData(d.employeeId);

      // I need to execute this only after getData has been processed...
      this.$store.dispatch('fetchHistory');
    },

所以我想做的是这样的:

代码语言:javascript
复制
selectEmployee(d) {

          this.getData(d.employeeId).then(() => {
             // check data here and then execute the fetch
             this.$store.dispatch('fetchHistory');
          });

        },

但我在then安全中得到错误,似乎那里是不允许的。

有什么线索吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-25 03:24:38

getData中返回promise,然后你就可以做getData().then()

代码语言:javascript
复制
getData(id){
  return this.$store.dispatch('getData', {
            employeeId: this.employeeId
         }).then(() => {
           if (this.employeeData.length) {
              // here some code...
           }
         });
}
票数 2
EN

Stack Overflow用户

发布于 2018-07-25 03:34:10

您还可以使用ES6s async await,而不是链接Promise,这使得代码更简洁、更具可读性。

代码语言:javascript
复制
async getData(id) {
    await this.$store.dispatch('getData', {
        employeeId: id,
    });

    if(this.employeeDate) //do something
},

async selectEmployee(d) {
    await this.getData(d.employeeId);

    // I need to execute this only after getData has been processed...
    this.$store.dispatch('fetchHistory');
},
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51506116

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档