Skip to content

实现sleep函数 #38

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Twlig opened this issue Mar 16, 2022 · 0 comments
Open

实现sleep函数 #38

Twlig opened this issue Mar 16, 2022 · 0 comments

Comments

@Twlig
Copy link
Owner

Twlig commented Mar 16, 2022

sleep函数

使计算机程序进入休眠。当函数设计的计时器到期,程序继续执行。

  1. Promise.then

function sleep(delay) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, delay);
    })
}
sleep(1000).then(() => {
    console.log("sleep结束")
})
  1. async await

function sleep(delay) {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            resolve();
        }, delay);
    })
}
async function test() {
    console.log("开始");
    await sleep(1000);
    console.log("结束")
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant