npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

sleep-monster

v0.0.3

Published

一个优雅的JavaScript sleep库,包含同步方式、异步方式即阻塞线程和非阻塞线程方式的sleep。

Readme

sleep-monster

一个优雅的JavaScript sleep库,包含同步方式、异步方式即阻塞线程和非阻塞线程方式的sleep

背景

这是一个不太正经的背景介绍。请理性阅读。

  • 你可能遇到过这样的场景,项目打包部署之后,响应速度很快,网站性能很好,那么你一定会非常苦恼,因为这样的话,你没有什么理由能够让客户加钱了。而且因为现在项目的优化必须要提前做好,你也很难自己在写一套不优化的代码,那么我想sleep-monster可以帮到你。只需要引入函数、调用函数,几行代码即可让项目陷入沉睡任意时间,不需要改动你项目的任何配置代码,等到客户加钱之后,去掉这几行代码即可,sleep-monster拥有很好的灵活性。

  • 此外,sleep-monster也是渐进式的,sleep-monster提供了很多方法,你可以利用这些方法逐步降低沉睡的时间,来达到多次让客户加钱的目的。

  • 友情提示:如果遇到非常棘手并且冥顽不灵的客户,建议直接使用syncSleepYears函数,让客户长长记性。

安装

npm install sleep-monster

使用

注意:

  1. sleep-monster将sleep函数分为了同步方式和异步方式两种。同步方式的sleep会阻塞线程,由于JavaScript是单线程的,所以这些同步方法会将所有的同步任务和异步任务阻塞,并且非常消耗性能,请谨慎使用,如果数值过大,轻则Chrome爆炸、电脑死机、风扇呼呼响,重则机毁人亡😂。

  2. 异步方式的sleep不会阻塞线程,也不会造成严重的性能问题,但是异步的sleep只能阻塞其当前作用域,并不是真正意义上的阻塞。

支持的方法

默认推荐使用asyncSleepsyncSleep这两个函数,默认参数单位是毫秒。所有可用函数如下:

异步函数(非阻塞线程):

  • asyncSleep 单位:毫秒
  • asyncSleepSeconds 单位:秒
  • asyncSleepMinutes 单位:分钟
  • asyncSleepHours 单位:小时
  • asyncSleepDays 单位:天
  • asyncSleepMonths 单位:月
  • asyncSleepYears 单位:年

同步函数(阻塞线程):

  • syncSleep 单位:毫秒
  • syncSleepSeconds 单位:秒
  • syncSleepMinutes 单位:分钟
  • syncSleepHours 单位:小时
  • syncSleepDays 单位:天
  • syncSleepMonths 单位:月
  • syncSleepYears 单位:年

异步的sleep使用

异步的sleep可以配合async/await或者Promise使用。

async/await

import { asyncSleep } from 'sleep-monster'

const test = async () => {
  console.log('go to sleep...')
  const res = await asyncSleep(2000)
  /* sleep之后需要执行的代码 */
  console.log(res) // true
  console.log('2000ms later now')
}
test()

console.log('The synchronized code is still executed') // 无法阻塞其作用域之外的代码执行

Promise

import { asyncSleep } from 'sleep-monster'

const test = () => {
  console.log('go to sleep...')
  asyncSleep(2000).then(res => {
    /* sleep之后需要执行的代码 */
    console.log(res) // true
    console.log('2000ms later now')
  })
}
test()

console.log('The synchronized code is still executed') // 无法阻塞其作用域之外的代码执行

同步的sleep使用

同步的sleep直接调用函数即可。

import { syncSleep } from 'sleep-monster'

setTimeout(() => {
  console.log('3000ms') // 异步任务也被阻塞
}, 3000)

syncSleep(5000)

console.log('5000ms later now.Both synchronous and asynchronous code are blocked.') // 所有的同步任务和异步任务都被阻塞了