【Promise】某个异步方法执行结束后 在执行下面方法

使用Promise ,当 layer.msg('查询成功') 这个方法执行结束后 ,下面代码才会执行

javascript 复制代码
let thas = this
async function showMessage() {
    await new Promise(resolve => layer.msg('查询成功', resolve));
    // 这里的代码将在 layer.msg 执行结束后执行
    thas.isGuarantee = true;
    thas.InsurancePolicyInfo = res.data;
    thas.ifTimeClose();
}
showMessage();

注意事项:Promise内如果使用 this , this无法访问到Vue实例 ;
解决方法:
1.你需要在外部定义全局变量 值是this, 在promise内使用这个变量

2.直接调用Vue组件实例
描述:箭头函数不会改变 this 的指向,它会捕获外部上下文的 this。因此,在箭头函数内部,this 会指向 Vue 组件的实例

javascript 复制代码
async showMessage() {
  await new Promise(resolve => layer.msg('查询成功', resolve));
  this.isGuarantee = true;
  this.InsurancePolicyInfo = res.data;
  this.ifTimeClose();
}

// 在 Vue 组件中调用 showMessage
this.showMessage();
相关推荐
我是李武涯3 分钟前
从`std::mutex`到`std::lock_guard`与`std::unique_lock`的演进之路
开发语言·c++
前端大卫31 分钟前
为什么 React 中的 key 不能用索引?
前端
你的人类朋友33 分钟前
【Node】手动归还主线程控制权:解决 Node.js 阻塞的一个思路
前端·后端·node.js
史不了1 小时前
静态交叉编译rust程序
开发语言·后端·rust
读研的武1 小时前
DashGo零基础入门 纯Python的管理系统搭建
开发语言·python
Andy2 小时前
Python基础语法4
开发语言·python
但要及时清醒2 小时前
ArrayList和LinkedList
java·开发语言
孚亭2 小时前
Swift添加字体到项目中
开发语言·ios·swift
hweiyu002 小时前
Go、DevOps运维开发实战(视频教程)
开发语言·golang·运维开发