async与await

/**
 *传入参数n,表示这个函数需要执行的时间(毫秒)
 *得到的结果是n + 200,这个值将用于下一步骤
 */
function takeLongTime(n) {
    return new Promise(resolve => {
        setTimeout(() => resolve(n + 200), n)
    })
}

function stemp1(n) {
    console.log(`stemp1 with ${n}`)
    return takeLongTime(n);
}

function stemp2(n) {
    console.log(`stemp2 with ${n}`)
    return takeLongTime(n);
}

function stemp3(n) {
    console.log(`stemp3 with ${n}`)
    return takeLongTime(n);
}


async function doIt() {
    console.log('doIt');
    console.time('doIt');
    const time1 = 300;
    const time2 = await stemp1(time1);
    const time3 = await stemp2(time2);
    const result = await stemp3(time3);
    console.log(`result is ${result}`)
    console.timeEnd('doIt');
}
doIt();
原文地址:https://www.cnblogs.com/ssszjh/p/13188172.html