实现 sleep 函数

实现 sleep 函数

const sleep = (milliseconds) => {
  return new Promise(resolve => setTimeout(resolve, milliseconds))
}

如何使用:

sleep(500).then(() => {
  //do stuff
})

async 函数中使用:

const doSomething = async () => {
  await sleep(2000)
  //do stuff
}

doSomething()