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

async-adapter

v0.0.0

Published

Adapt callback pattern functions into async-await ready functions

Downloads

6

Readme

asyncAdapter

Take functions with a callback pattern and use them with async/await!

The Problem

Let's say you have a legacy function that creates an XMLHttpRequest which passes response data to a callback function, for example:

function httpGET(endpoint, callback) {
  var xhr = new XMLHttpRequest();
  xhr.addEventListener("readystatechange", function() {
    if (this.readyState == 4 && this.status == 200) {
      callback(JSON.parse(xhr.responseText));
    }
  });
  xhr.open("GET", endpoint);
  xhr.send();
}

It has been a trusty sidekick, but it is a little outdated and makes using retrieved data more involved than modern Javascript needs to be.

You want to use the latest and greatest APIs that tc39 and Babel can provide– like async/await or the Promise API– and callbacks just don't cut it.

What could you do?

The Solution

Enter asyncAdapter. This nifty utility magically makes the function into a new Promise-based function, allowing it to be await-ed or otherwise handled like a Promise; this is achieved by passing in the Promise's resolve argument where the original function's callback would go.

(Okay so it is not exactly magic, but it's still pretty cool)

Here is how you would use the example function above with asyncAdapter:

const asyncHttpGET = asyncAdapter(httpGET,'https://example.com/api/data');

(async function someAsyncFunction() {
  const data = await asyncHttpGET;
  console.log(data); // -> { foo: 'bar' }
})()

The first argument to the adapter is the original function name, and the rest of the arguments constitute any arguments you would pass to the original function, in the same order.

Note that you should not pass a function into the asyncAdapter arguments, unless that function can return a value (e.g. not for an AJAX/Promise-based function).

Here is an example of a non-asynchronous function being used with asyncAdapter:

// Original function
const add = (n1,n2,callback) => callback(n1 + n2);

// Add callback function to return value
const asyncSum20 = asyncAdapter(add,10,10,n => n);

// Add callback function to return value with side effects
const asyncSum50 = asyncAdapter(add,10,10,n => n + 30);

// Use inside function to create DRY async version of original function
const asyncSum = (n1,n2,n3) => asyncAdapter(add,n1,n2, n => n + n3);

(async function someAsyncFunction() {
  const sum20 = await asyncSum20;
  const sum50 = await asyncSum50;
  const sum100 = await asyncSum(5,20,75);

  console.log(sum20); // -> 20
  console.log(sum50); // -> 50
  console.log(sum100); // -> 100
})