Home > Mobile >  Headaches: a small program called Promise don't wait for the result, for advice.
Headaches: a small program called Promise don't wait for the result, for advice.

Time:09-25

Small program many interfaces are asynchronous, I searched for online information, adopt the method of the promise:

Defined in the app. Js an authorization registration function:
 
Reg: function (action)//authorization, there's a few interfaces are wxapi interface, search on the net with the normal, can step in accordance with the
{
The console. The log (' 1 r - start authorization: + action)
Var the userInfo.
var that=this;
Var memcode=' ';

Utils2. GetSetting (). Then (res=& gt; {
The console. The log (' getSetting - & gt; ', res);
Return utils2. GetUserInfo ();
}). Then (res=& gt; {
The console. The log (' the userInfo - & gt; ', res);
The userInfo: res. The userInfo,
That. GlobalData. The userInfo=res. The userInfo
The console. The log (' 2 r - get the userinfo: + the userinfo)
Return utils2. The login ();
}). Then (res=& gt; {
The console. The log (' 3 r - direct return request code: '+ res. Code)
That. GlobalData. Memcode=res. Code;
Memcode=res. Code;
The console. The log (' 11 r - authorization is done:)
Return true;
})
return false;
},

=======================
If I call this function, on the front page directly without waiting for a return value, there is no way to success in accordance with the, I think for a moment, difficulty make a promise, but still won't do
Try to 1:
 
Const p1=new Promise ((resolve, reject)=& gt; {
Var SSS=app. Reg ();

If (SSS) {
The console. The log (' authorization to complete 1)
}
The else
The console. The log (' authorization to complete 2 ')

});

Try 2
 
Const p1=new Promise ((resolve, reject)=& gt; {
Var SSS=app. Reg ();
If (SSS) {
Resolve (' success ')
The console. The log (' 11111 ');
} else {
The console. The log (' 2222 ');
Reject (" err ")
}
});

P1. Then (data=> {
https://bbs.csdn.net/topics/The console. The log (' p1=', data);//hello,
Return the data + 'world';
The console. The log (' authorization is completed)
}, err=& gt; {
console.log(err);//returns the reject (value) the value of the
Return err + 'err;
})

===============
Two code ranging results are directly returns false, I to the deep understanding of the promise, there is no way, emergency help,
The code may be primary, everybody don't laugh at me, welcome to grant instruction!!




CodePudding user response:

As if they solved, make a promise and return in the reg and promise, (can't return true or false)

Reg: function (action)//authorization, there's a few interfaces are wxapi interface, search on the net with the normal, can step in accordance with the
{
The console. The log (' 1 r - start authorization: + action)
Var the userInfo.
var that=this;
Var memcode=' ';
Let promise=new promise ((resolve, reject)=& gt; {
Utils2. GetSetting (). Then (res=& gt; {
The console. The log (' getSetting - & gt; ', res);
Return utils2. GetUserInfo ();
}). Then (res=& gt; {
The console. The log (' the userInfo - & gt; ', res);
The userInfo: res. The userInfo,
That. GlobalData. The userInfo=res. The userInfo
The console. The log (' 2 r - get the userinfo: + the userinfo)
Return utils2. The login ();
}). Then (res=& gt; {
The console. The log (' 3 r - direct return request code: '+ res. Code)
That. GlobalData. Memcode=res. Code;
Memcode=res. Code;
The console. The log (' 11 r - authorization is done:)
Resolve (true);
}). The catch (err=& gt; {
The console. The log (' 12 - error: r - authorization has been completed ', err)
Reject (false);
});
Return promise;
},


In the calling side so write
The console. The log (' start authorization)
App. Reg (). Then ((res)=& gt; {
The console. The log (' authorization succeeded, res);
Wx. ShowToast ({
Title: 'authorization success, thanks to support,
Icon: "success"
})
}, (error)=& gt; {
The console. The log (' login failed: '+ error);
.
})

To, looking forward to prawns can give better solution ~ ~

CodePudding user response:

Promise not to use, in the promise is synchronous, suggest p1 use async await to write, and then the var SSS=await app. Reg ()
  • Related