async&await

51 阅读1分钟
function setTime(t) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, t);
    })
}
        // async 异步函数  是es7才有的与异步函数相关的关键字
        (async function() {
            console.log(1);
            // async是用来定义一个异步函数 搭配await 来等待一个promise对象   它只能在异步函数async function 内部使用
            await setTime(1000);
            await setTime(1000);
            await setTime(1000);
            await setTime(1000);
            console.log(2);
        })()
;(async function() {
            var res1 = await fetch('https://api.tf2sc.cn/api/tfcar/car/model', {headers: {Platformtype: 'h5'}}).then(res => res.json());
            console.log('res1', res1);

            var res2 = await fetch('https://api.tf2sc.cn/api/tfcar/car/price?', {headers: {Platformtype: 'h5'}}).then(res => res.json());
            console.log('res2', res2);

            var res3 = await fetch('https://api.tf2sc.cn/api/tfcar/car/list?page=1&sort=', {headers: {Platformtype: 'h5'}}).then(res => res.json());
            console.log('res3', res3);
        })()