Js asynchronous programming scheme, in this summary
  • promise

  • async/await

    1.asyncThe async () function is used to declare an asynchronous function that returns onePromiseObject. PS: If byasyncNot included in the declared functionawaitKeyword, then the execution of the function is no different from ordinary functions.2.PromiseThen success corresponds to successawait
    async function test1(){
        let p1 = Promise.resolve(3);
        //p1.then((data)=>{
        // console.log(data)
        / /})
        const data = await p1;// This line of code has the same meaning as commented out above
        console.log(data) 
    }
    test1()
    
    3.Promise. Catch Corresponds to the exceptiontry catch
    async function test2(){
        let p1 = Promise.reject(3);

        try{
            const data = await p1;
            console.log(data); 
        }catch(e){
            console.error('e:',e);
        }
    }
    test2();
Copy the code
  • Generator function