async和await

await is followed by a function that will return new promise and execute it
await can only be placed in async functions
use async and await to get a successful result
function shake dice(){ return new Promise((resolve, reject)=>{ let sino = parseInt(Math.random() * 6 +1) setTimeout(()=>{ resolve(sino) },3000) }) } async function test(){ let n =await dice() console.log( n) } test() In the async of the above code, await dice() is executed first, and after three seconds of execution, the result is assigned to the left n, that is to say, the test function takes three seconds to execute Completed, so the test function is asynchronous, so async must be written in front












Get failed result
function shake dice(guess){ return new Promise((resolve, reject)=>{ let sino = parseInt(Math.random() * 6 +1) if(sino > 3){ if(guess= == 'big'){ resolve(sino) }else{ reject(sino) } }else{ if(guess === 'big'){ reject(sino) }else{ resolve(sino) } } setTimeout(() =>{ resolve(sino) },300) }) } async function test(){ try{ //Put await and the operation of getting its value in try let n =await shake dice('big') console .log('win' + n) }catch(error){ //The failed operation is placed in the catch console.log('lost' + error)




























}
}
test()
puts await and successful operations in try, and fails in catch

Why use await
in order to make our asynchronous code more like synchronous code

There are multiple promises, how do I get the result after all promises are completed? For
example, there are two dice, I want to get the points of these two dice

Use promise
function to shake dice(guess){ return new Promise((resolve, reject)=>{ let sino = parseInt(Math.random() * 6 +1) if(sino > 3){ if(guess === 'big'){ resolve(sino) }else{ console.log('error') reject(sino) } }else{ if(guess === 'big'){ console.log('error') reject(sino ) }else{ resolve(sino) } } setTimeout(()=>{ resolve(sino) },300) }) } Promise.all([shaker('big'),shaker('big') ]).then((x)=>{console.log(x)},(y)=>{console.log(y)})























promise.all is followed by an array, each item of the array is a function call that returns a promise, the first parameter of then is to call after all promises are successful, and the result of getting all promises is an array; the second parameter Get the first value that fails

Using await
await directly obtains the results of multiple promises, because Promise.all() also returns a promise, so if you want to use await to get the values ​​of multiple promises, you can directly await Promise.all()
async function test() { try{ let n = await Promise.all([shaker('big'),shaker('big')]) console.log(n) }catch(error){ console.log(error) } } test() The async function will return a promise, and the state value of the Promise object is resolved (successful). If you do not write return in the async function, then the value of the Promise object resolve is undefined









If you write return, the value of return will be the value you passed in when you succeeded

What does await do after waiting?
Then the result of the expression on the right is what await is waiting for.
After waiting, for await, there are 2 cases

It is not a promise object , it
is a promise object.
If it is not a promise, await will block the following code, first execute the synchronization code outside async, after the synchronization code is executed, return to the inside of async, and use this non-promise thing as the result of the await expression.
If it waits for a promise object, await will also suspend the code behind async, execute the synchronization code outside async first, wait for the promise object to fulfill, and then use the resolve parameter as the result of the await expression.

If the code in asycn is synchronous, then this function will be called synchronously
async function fn(){ console.log('a') } fn() console.log('b') //a // b If the promises connected after await are all synchronous, the following promises will be executed synchronously, but you still have to wait to get the value (special note: if the promise does not have a successful value passed in, it is considered a failure for await , the following code will not be executed), so no matter whether the code behind await is synchronous or asynchronous, await always takes time, executes from right to left, executes the code on the right first, and finds the await keyword after execution , so let go of the thread and block the code function fn(){ return new Promise(resolve=>{ console.log(1) }) } async function f1(){ await fn() console.log(2) } f1() console.log(3) //1 //3 This code prints 1 first, then 3 because fn is synchronous, but because there is no resolve result, await cannot get the value, so 2 will not be printed




















function fn(){ return new Promise(resolve=>{ console.log(1) resolve() }) } async function f1(){ await fn() console.log(2) } f1() console.log(3 ) //1 //3 //2 This code has one more resolve than the previous one, indicating that the promise is successful, so await can get the result, so it is 1 3 2














Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324342764&siteId=291194637