通过Promise
实现sleep
函数,区别于循环,不堵塞线程
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function test() {
console.log('start test.');
await sleep(3000);
console.log('end test.');
}
test();
console.log('continue execute!');
网友评论