async 簡單總結

2021-09-24 06:25:12 字數 1226 閱讀 8994

async形式上的表現

async function helloasync()

console.log(helloasync()) // -->promise 返回乙個promise物件

複製**

因為上面返回乙個promise物件,所以可以使用then*/
async function helloasync()

helloasync().then(v=>)

複製**

function awaitdemo(),100)})}

async function helloasync()

helloasync();

複製**

1.awaitdemo() 返回乙個promise物件

2.helloasync() 中 await awaitdemo();表示在這裡要等待非同步操作完成才會執行console.log;

3. 執行結果:await

helloasync

複製**

4.既然是返回promise物件,所以可以返回rejected,如果返回rejected 那麼console.log("helloasync");就不會執行了
function awaitdemo()

async function helloasync()

helloasync().then(v=>).catch(e=>);

複製**

await補充:await後可以是str,boolean,num,非非同步func
function awaitdemo2(), 100);

}async function helloasync()

helloasync(); //--->helloasync

// await2 不再等待settimeout,所以先列印helloasync

複製**

function one(),500)

});}

function two(),500)

});}

function three(),500)

});}

function four(),500)

});}

async function task()

task();

複製**

async 簡單總結

async形式上的表現async function helloasync console.log helloasync promise 返回乙個promise物件 複製 因為上面返回乙個promise物件,所以可以使用then async function helloasync helloasync...

async 簡單總結

async形式上的表現async function helloasync console.log helloasync promise 返回乙個promise物件 複製 因為上面返回乙個promise物件,所以可以使用then async function helloasync helloasync...

await與async簡單理解

使用 async 可以定義乙個 非同步函式 async 函式的實際返回值總是乙個 promise 物件 await的意思就是等待。它後面可以跟乙個表示式。如果是值 如字串 數字 普通物件等等 的話,返回值就是本身的值。await 操作符用於等待乙個 promise 物件,它只能在非同步函式 asyn...