async function t1(){
    let a = await 'dongdong';
    console.log(a);
}
t1();
async function t2(){
    let a = await new Promise((resolve)=>{});
    console.log(a);
}
t2();
async function t3(){
    let a = await new Promise((resolve)=>{
        resolve();
    });
    console.log(a);
}
t3();
async function t4(){
    let a = await new Promise((resolve)=>{
        resolve('hello dongdong');    });
    console.log(a);
}
t4();
async function t5(){
    let a = await new Promise((resolve)=>{
        resolve('hello');
    }).then(()=>{
        return 'lala';
    });
    console.log(a);
}
t5();

async function t6(){
    let a = await fn().then(res=>{
        return res;
    });
    console.log(a);
}
async function fn(){
    await new Promise((resolve)=>{
        resolve('dongdong');    }); 
}
t6();

async function t7(){
    let a = await fn().then(res=>{
        return res;
    });
    console.log(a);
}
async function fn(){
    await new Promise((resolve)=>{
        resolve('dongdong');    }); 
    return 'lala';
}
t7();
Copy the code

one by one

async function t1(){ let a = await 'dongdong'; console.log(a); //dongdong } t1();Copy the code

Await is an expression that returns the corresponding value if it is not followed by a promise object. Async function t1(){let a = dongdong’; console.log(a); } t1();

async function t2(){ let a = await new Promise((resolve)=>{}); console.log(a); // } t2();Copy the code

Resolution: Await: if await is a promise object, await will wait for the resolve state value of the promise object and return the value to the preceding variable. The state of the Promise object is pending and there is no resolve state value. So you can’t print anything.

async function t3(){ let a = await new Promise((resolve)=>{ resolve(); }); console.log(a); //undefined } t3();Copy the code

Resolution: Await: if await is a Promise object, await will await the resolve state value of the promise object and return the value to the preceding variable. The promise state is resolve and undefined. So it’s just going to print undefined.

async function t4(){ let a = await new Promise((resolve)=>{ resolve('hello dongdong'); }); console.log(a); //hello dongdong } t4();Copy the code

Resolution: Await if await is a promise object, await will await the promise object’s resolve state value and return the value to the preceding variable. The promise object’s state is resolve. And its state value is’ Hello dongdong’, so it prints hello Dongdong.

async function t5(){ let a = await new Promise((resolve)=>{ resolve('hello'); }).then(()=>{ return 'lala'; }); console.log(a); //lala } t5();Copy the code

If a promise object is await, await will wait for the value of the Promise’s resolve state and return the value to the preceding variable. The promise object’s state value is a resolve state, and its state value is’ Hello ‘. This is followed by the execution of a then method, which returns a brand new Promise object and the value of resolve in the brand new Promise. , so the final print is ‘lala’.

async function t6(){ let a = await fn().then(res=>{ return res; }); console.log(a); //undefined } async function fn(){ await new Promise((resolve)=>{ resolve('dongdong'); }); } t6();Copy the code

Parsing: Async function execution returns a Promise object, and the value returned inside async function is the value of the promise’s resolve state.

javaScript async function fn(){ return ‘a’; } console.log(fn()); //Promise {

: “a”} //__proto__: Promise //[[PromiseStatus]]: “fulfilled” // [[PromiseValue]]: “A” first considers that the fn() implementation returns a promise object, and since the FN implementation does not return a value, the promise object’s state resolve is undefined. Undefined is used as an argument to the callback function in the next THEN, so the print result is undefined.

async function t7(){ let a = await fn().then(res=>{ return res; }); console.log(a); //lala } async function fn(){ await new Promise((resolve)=>{ resolve('dongdong'); }); return 'lala'; } t7();Copy the code

Parsing: Async function execution returns a Promise object, and the value returned inside the async function is the value of the Promise object’s resolve state. Fn () returns a Promise object, and fn returns ‘lala’, so the promise’s resolve state is ‘lala’. And take ‘lala’ as the argument in the next THEN callback, so the print result is lala.

conclusion

The async function executes and returns a Promise object that returns the value of the Promise object’s resolve state. Await is returned directly if it is not a promise object after await. Await followed by a promise returns the value of the resolve state of the Promise object. The reject state is not considered.

If you have the chance to read this article, welcome to correct ha