async 函數的使用

基本使用

async函數返回的是一個Promise,可以進行Promise的相關操做,函數內部返回的結果會成爲then方法回調函數的參數promise

async function asyncfunc(){
    return "這是一個async函數"
}
asyncfunc()
asyncfunc().then(function(info){console.log(info)})

95{PHIL1A0NG7){RQL[0)3X.png

當函數執行中遇到await,需等異步操做完成,纔會執行後面的代碼異步

async function asyncfunc() {
        let now = new Date().getTime();
        await new Promise((resolve, reject) => {
            setTimeout(() => {
                resolve()
            }, 2000)
        })
        console.log(new Date().getTime() - now)
    }

`DNGPR(45GH}YEV7899)BUV.png

函數內部錯處,返回的promise狀態爲rejectedasync

async function asyncfunc(){   
    console.log(err)
    }

D1Q3)IMTP04VBN4)38`G6A9.png

async函數返回的Promise只有函數內部中await後的異步事件執行完後,纔會改變,除非中途return或者出錯函數

(async function() {
        await new Promise((resolve, reject) => {
            console.log("第一個await")
            resolve()
        })
        await new Promise((resolve, reject) => {
            console.log("第二個await")
            resolve()
        })
        await new Promise((resolve, reject) => {
            console.log("第三個await")
            resolve()
        })
    })().then(info=>{console.log("觸發then")})

N9E6F9{3DX{7QEM9(~AXB}3.png

(async function() {
        await new Promise((resolve, reject) => {
            console.log("第一個await")
            resolve()
        })
        return "執行return"
        await new Promise((resolve, reject) => {
            console.log("第二個await")
            resolve()
        })
        await new Promise((resolve, reject) => {
            console.log("第三個await")
            resolve()
        })
    })().then(info=>{console.log("觸發then")})

BV5NI6)R{M`VASF4QZOC]{V.png

(async function() {
        await new Promise((resolve, reject) => {
            console.log("第一個await")
            resolve()
        })
        throw new Error("出錯了")
        await new Promise((resolve, reject) => {
            console.log("第二個await")
            resolve()
        })
        await new Promise((resolve, reject) => {
            console.log("第三個await")
            resolve()
        })
    })()

YR6R)2{5BWK3)GSRJAM9OAC.png

await後的Promise狀態變爲rejected時,會被catch接收到spa

(async function() {
        await new Promise((resolve, reject) => {
            reject("狀態變爲rejected")
        })
    })().catch(info => {
        console.log(info)
    })

8U04F]P~[%CZZMV833VIVOK.png

任意一個await後的Promise函數變爲rejecte,async函數的執行就會中斷,若想繼續執行,可以使用try{}catch(e){}捕獲3d

(async function() {
        await new Promise((resolve, reject) => {
            reject("狀態變爲rejected")
        })
        await new Promise((resolve, reject) => {
            console.log("第二個await")
            resolve()
        })
    })().catch(info => {
        console.log(info)
    })

}0ZO@]T]M1~9CKC_RC9LHDH.png

(async function() {
        try {
            await new Promise((resolve, reject) => {
                reject("狀態變爲rejected")
            })
        } catch (err) {
            console.log("捕獲" + err)
        }

        await new Promise((resolve, reject) => {
            console.log("第二個await")
            resolve()
        })
    })().catch(info => {
        console.log(info)
    })

A9$NR22BA[ROY}XW9W7(MZU.png
另外一種方法是將await後面的Promise添加catchcode

(async function() {

        await new Promise((resolve, reject) => {
            reject("狀態變爲rejected")
        }).catch(info => {
            console.log("捕獲" + info)
        })
        await new Promise((resolve, reject) => {
            console.log("第二個await")
            resolve()
        })
    })().catch(info => {
        console.log(info)
    })

L2I$\]3\[$6WN0$~$}ULQ]Z9T.png

相關文章
相關標籤/搜索