this.$confirm里面使用await异步调取接口数据 或者 异步接口then里面使用await/promise

this.$confirm里面使用await异步调取接口数据 或者 异步接口then里面使用await/promise
只需在then的回调方法/匿名方法里添加一个async标记就可以了。

xxx.then(async () => {
    var workOrder = await this.getWorkOrderByNo(productTrace.workOrderNo);
})

原理:同一作用域内async才能使用await。

再看下面的例子,可以实现每隔1秒执行一次任务。即实现Sleep。

            for(var item of data) {
                await new Promise(async (resolve) => {
                    this.batchPrintBoxCode(
                        item.boxCode,
                        item.goodsCode,
                        batchNo,
                        item.qty,
                        item.creationTime,
                        item.creatorUserId,
                        this.boxCodeInfoObj.specification,
                        '',
                        item.seriesNo,
                        item.lotNo
                    );
                    setTimeout(() => {
                        resolve("ok");
                    }, 1000);
                });
            }
            this.reprintBoxCodeBtnLoading = false

 

posted @ 2022-06-22 18:02  屌丝大叔的笔记  阅读(1073)  评论(0编辑  收藏  举报