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

小程序 文章 2022-03-10 14:20 406 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