通過Promise
實(shí)現(xiàn)sleep
函數(shù)变擒,區(qū)別于循環(huán),不堵塞線程
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肾筐!');