Home > front end >  Js how the asynchronous to synchronous
Js how the asynchronous to synchronous

Time:10-09

There are ways to aaa asynchronous return results ret=1
The function aaa () {
SetTimeout ()=& gt; {
Let ret=1
}, 2000)
}

I want to use the method of BBB synchronous returns the result of the aaa encapsulates the
Let ret=BBB ()

I've tried async await the results returned by the promise, yield returns undefined
Under which a great god can help

CodePudding user response:

 
The function aaa () {
The return of new Promise ((reslove, reject)=& gt; {
SetTimeout ()=& gt; {
Reslove (1)
}, 2000)
}). Then (res=& gt; {
Return res
})
}

Let a=await aaa ()

CodePudding user response:

 
The function aaa (res) {
SetTimeout ()=& gt; {
Let ret=1;
Res (ret);
}, 2000)
}

The function BBB () {
The return of new Promise (reslove=& gt; Aaa (reslove))
}

! Async function () {
Var result=await BBB ();
console.log(result);
} ();
  • Related