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

@nxtedition/yield

v1.0.23

Published

Cooperative yielding for Node.js to keep servers and applications responsive.

Readme

@nxtedition/yield

Cooperative yielding for Node.js to keep servers and applications responsive.

When using synchronous APIs like DatabaseSync, sync fs API's such as fs.readFileSync, or fs.statSync — or simply performing lots of synchronous computation — the event loop is blocked and cannot process I/O, timers, or incoming requests. Even long-running chains of asynchronous microtasks (e.g. deeply nested Promise.then chains) can starve the event loop in the same way.

This library provides a simple mechanism to periodically yield control back to the event loop during these long-running operations, preventing event loop starvation and keeping your application responsive.

Install

npm install @nxtedition/yield

Usage

Promise-based

import { maybeYield } from '@nxtedition/yield'
import { DatabaseSync } from 'node:sqlite'

const db = new DatabaseSync('data.db')

for (const row of db.prepare('SELECT * FROM large_table').iterate()) {
  processRow(row)
  const yielded = maybeYield()
  if (yielded) {
    await yielded // give the event loop a turn
  }
}

Callback-based

import { shouldYield, doYield } from '@nxtedition/yield'
import fs from 'node:fs'

function processFiles(files, i = 0) {
  for (; i < files.length; i++) {
    if (shouldYield()) {
      doYield((index) => processFiles(files, index), i)
      return // resumes at index i after the event loop has had a turn
    }
    const data = fs.readFileSync(files[i])
    transform(data)
  }
}

Unconditional yield

import { doYield } from '@nxtedition/yield'

// Promise-based
await doYield()

// Callback-based
doYield((opaque) => {
  continueWork(opaque)
}, data)

Checking without yielding

import { shouldYield } from '@nxtedition/yield'

while (hasWork()) {
  if (shouldYield()) {
    break // pick the remaining work back up on a later turn
  }
  doWork()
}

API

shouldYield(timeout?): boolean

Returns true when more than timeout milliseconds (default: 40) have elapsed since the last yield dispatch. Does not yield — only checks whether yielding is recommended.

Once it returns true it latches and keeps returning true until the pending yield dispatch has run. When it latches, a dispatch is scheduled automatically, so the latch clears after the next event-loop turn even if the caller never enqueues a yield.

maybeYield(timeout?): Promise<void> | null

If a yield is needed, queues a yield and returns a Promise. Otherwise returns null. Use this in async functions.

maybeYield(callback, opaque?, timeout?): void

If a yield is needed, defers callback(opaque) to after the yield. Otherwise calls callback(opaque) synchronously.

Note: because the no-yield path invokes the callback synchronously, a recursive continuation-passing loop built on this overload grows the stack until the timeout elapses. For long loops prefer the shouldYield()/doYield() pattern shown above.

doYield(): Promise<void>

Unconditionally queues a yield and returns a Promise that resolves after the event loop has been given a turn.

doYield(callback, opaque?): void

Unconditionally defers callback(opaque) to after the next yield.

setYieldTimeout(timeout): void

Set the default yield timeout in milliseconds. Must be a non-negative number (NaN is rejected; Infinity disables time-based yielding). Default: 40.

setYieldDispatcher(dispatcher): void

Override the scheduling function used to defer work. The dispatcher receives a callback and should invoke it asynchronously (e.g. via setTimeout or setImmediate) — asynchronous invocation is what actually gives the event loop a turn. A dispatcher that invokes the callback synchronously is tolerated and loses no queued items, but it defeats the purpose of yielding. Pass null (or undefined) to restore the default dispatcher. Throws a TypeError for any other non-function value. If the dispatcher itself throws, the error propagates to the caller and the library recovers on the next scheduling attempt.

How it works

The library tracks when the last yield occurred using performance.now(). When shouldYield() detects that more than timeout milliseconds have elapsed, it signals that a yield is due. doYield and maybeYield batch pending callbacks into a queue that is drained on the next event-loop turn (via setImmediate by default), yielding again if the timeout is exceeded during draining.

License

MIT