代码之家  ›  专栏  ›  技术社区  ›  VAAA

VueJS函数

  •  0
  • VAAA  · 技术社区  · 7 年前

    我有以下方法:

        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');
        },
    

    所以我想这样做:

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

    但我发现 then 似乎那里不允许有合流。

    有线索吗?

    2 回复  |  直到 7 年前
        1
  •  1
  •   akuiper    7 年前

    兑现诺言 getData ,那么你可以 getData().then() :

    getData(id){
      return this.$store.dispatch('getData', {
                employeeId: this.employeeId
             }).then(() => {
               if (this.employeeData.length) {
                  // here some code...
               }
             });
    }
    
        2
  •  0
  •   Alan Smith    7 年前

    与链接承诺不同,您还可以使用ES6s async await,这使得代码更清晰可读。

    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');
    },
    
    推荐文章