实现sleep的效果

1. while循环的方式

function sleep(ms){
    let start = Date.now();
    let expire = start + ms;
    while(Date.now() < expire);
    console.log('111');
    return;
}

执行sleep(1000)之后,休眠了1000ms之后输出111。上述循环的方式缺点很明显,容易造成死循环。

2. 通过promise来实现

function sleep(ms){
    let temple = new Promise(
        (resolve) => {
            console.log(111);
            setTimeout(resolve, ms)
        });
    return temple;
}
sleep(500).then(function(){
    console.log(222);
})

先输出了111,延迟500ms后输出222

3. 通过async封装

function sleep(ms){
    return new Promise((resolve) => setTimeout(resolve, ms));
}
async function test(){
    let temple = await sleep(1000);
    console.log(1111);
    return temple;
}
test();

延迟1000ms输出了1111.

4. 通过generate来实现

function* sleep(ms){
    yield new Promise(function(resolve, reject){
        console.log(111);
        setTimeout(resolve, ms);
    })
}
sleep(500).next().value.then(function(){console.log(2222)})