如何解决函数里异步调用问题?

小程序 文章 2022-03-10 14:20 532 0 全屏看文

AI助手支持GPT4.0

如何解决函数里异步调用问题?How to solve the problem of asynchronous calls in functions?

如何解决函数里异步调用问题?

test:async function(){

    var that=this;

    console.log('0');

    const p=await new Promise(function(resolve,reject){            

      that.a();

      resolve();         

    })       

    console.log('2');

  },

  a:function(){    

    wx.showModal({

      showCancel: false,

      confirmText:'确定',

      content:'1',

      title:'提示',

      success:function(res){

        if(res.confirm){

          console.log('1');          

        }

      }

    })

如何在函数里调用另一个函数?目的是按顺序执行输出012,我试过直接将wx.showModal放到Promise里是可以按顺序输出的。

test:async function(){ var that=this; console.log('0'); const p=await new Promise(function(resolve,reject){ that.a(); resolve(); }) console. log('2'); }, a: Function() {Wx. Showmodal ({showcancel: false, confirmetext: 'OK', content: '1', Title: 'prompt', success: function (RES) {if (res.confirm) {console. Log ('1 ');}}}) How to call another function in a function? The purpose is to execute output 012 in sequence. I tried to directly convert Wx Showmodal can be output in order when it is put into promise.

回答:

老张:
test:async function(){    var that=this;    console.log('0');    const p=await this.a()     console.log('2');  },  a:async function(){        let res = await wx.showModal({      showCancelfalse,      confirmText:'确定',      content:'1',      title:'提示',    })        if(res.confirm){          console.log('1');                  }}
有赞:

在a函数里面接收resolve,在modal success中执行

Mr.Zhao:

a返回promise 然后await that.a()

刘绵绵是仙女:大佬你这个就是我正在用的,我都加上了promise
Mr.Zhao:想说啥

-EOF-

AI助手支持GPT4.0


国内超级便宜服务器

摸鱼人热门新闻聚合

钻级赞助商 我要加入

开发者在线工具

第三方支付技术请加QQ群

相关文章
wx.getMenuButtonBoundingClientRect().top在iphone11值
洪湖水啊浪打浪!微信小商店啊静悄悄?
调不起授权弹窗?
小程序发布后搜索不到
ios 部分机型 微信内置浏览器打开h5页面 标题栏错位
随便看看
企业微信通讯录账号被管理员误/恶意删除,怎么办? 9568
问题? 9047
如何快速搭建抽奖助手小程序(无需代码知识) 6177
许涛 大哥在吗, 要解冻小程序的时候提示信息主体不一致, 能帮忙看下吗? 7471
小程序搜一搜全称搜索不显示 麻烦解决一下!谢谢 6792
我的小程序广告收款主体是个体工商户,每个月邮寄两次发票很是麻烦,我年底的时候打包邮寄一次可以么? 6126
如何解决渲染层网络错误Failed to load media? 17393
小程序找号找回 释放昵称? 3252
我的小程序广告主收入5301,实际进账4406,是怎么回事呀? 3108
删除部分微信用户个人数据的通知,怎么删除? 6380