如果用Generator 函数该怎么改写如下代码?

handleCurrentChange (val) {
    this.loading = true
    rtumQueryUser({
        body: {
            pageSize: 20,
            pageNum: val,
            userName: this.queryForm.userName
        }
    })
    .then(res => {
        this.loading = false
        if (res.data.code === '20000') {
            this.userList = res.data.result.data
            this.totalPage = res.data.result.totalPage
            this.currentPage = res.data.result.pageNum
            this.pageSize = res.data.result.pageSize
            // this.expandRowKeys.push(this.userList[0].userId)
        } else {
            this.$message(res.data.message)
        }
    })
    .catch(err => {
        this.loading = false
        this.$message.error(err.message)
    })
}

代码如上,想达到的效果是在 this.userList = res.data.result.data 使表格数据被渲染之后,再调用 this.expandRowKeys.push(this.userList[0].userId) 使表格的第一行展开,如果用Generator函数该怎么改写?也欢迎其他方法,多多益善,对这类问题不太会解决,谢谢!

阅读 1.9k
1 个回答

Generator 有能力,把所有“回调”改成真正的“返回”。

以前是:

asyncCall().then(function(response){ console.log(response) });

可以改成:

var response = yield gen.Task(asyncCall);
console.log(response);

细节:https://www.zouyesheng.com/js...

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题