Vue+TypeScript中await和async用法(代碼演示)

直接上代碼

import axios from 'axios'

const $http = axios.create({
    timeout: 100 * 1000,
});


/**
 * 入口
 */
async function startTest() {
    console.log(1)

    let result1 = await test1()

    console.log(2)

    let result2 = await test2()

    console.log(3)
}


/**
 * 測試1
 * 
 * 本地休眠3秒
 */
async function test1() {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve(true)
        }, 3000)
    })
}

/**
 * 測試2
 * 
 * 調用一個休眠十秒的遠程接口
 * 
 */
async function test2() {
    return $http.post("http://127.0.0.1:8080/sleep10000", {})
        .then((res: any) => {
            return "false";
        })
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章