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 🙏

© 2024 – Pkg Stats / Ryan Hefner

callback-to-async-iterator

v1.1.1

Published

Turn any callback-based listener into an async iterator.

Downloads

4,569

Readme

callback-to-async-iterator

Turn any callback-based listener into an async iterator.

We needed this module to turn our database listeners into async iterators, which is what GraphQL subscriptions expect to be passed. It might be useful for you too!

npm install callback-to-async-iterator

Usage

Imagine a standard callback-based listener like this:

// callback will be called with each new message added to the database
const listenToNewMessages = (callback) => {
  return db.messages.listen(message => callback(message));
}

The problem is that callbacks are push based, they push values to the listener whenever a new value is availabe. Async Iterators on the other hand are pull based, they request a new value and wait until it is available.

This module reconciliates that difference so you can turn your standard callback-based listener into an async iterator:

import asyncify from 'callback-to-async-iterator';

const messages = asyncify(listenToNewMessages);

// Wait until the first message is sent
const firstMessage = await messages.next();

// Asynchronously iterate over new messages and log them as they come in
for await (let message of messages) {
  console.log(message);
}

console.log('Done!')

This module will automatically buffer incoming data if .next hasn't been called yet.

Options

  • onClose: A function that's called with whatever you resolve from the listener after the async iterator is done, perfect to do cleanup
  • onError: A function that's called with any error that happens in the listener or async iterator
  • buffering: (default: true) Whether incoming values should be buffered in between requests for the next value in the async iterable (note: disabling this will make your iterator "lossy" if you don't immediately call .next())
asyncify(listenToNewMessages, {
  // Close the database connection when the async iterator is done
  // NOTE: This is passed whatever the listener resolves the returned promise with, in this case listenToNewMessages resolves with the database connection but it could be whatever you desire
  onClose: (connection) => { connection.close(); },
  // Log errors to your error tracking system
  onError: (err) => {
    errorTracking.capture(err);
  },
  buffering: false
})

Credits

This module is heavily based on the event emitter to async iterator utility used in graphql-js. Also big shoutout to @ForbesLindesay who helped a ton with the initial implementation and understanding the problem.

License

Licensed under the MIT License, Copyright ©️ 2017 Maximilian Stoiber. See LICENSE.md for more information.