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";
        })
}


你可能感兴趣的:(typescript,vue,nodejs,js,javascript)