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

wait-for-me

v1.0.1

Published

Returns a function you can await for (promisify), from a function which signature is (args..., err)

Downloads

3

Readme

wait-for-me

Return a function you can await for (promisify), from a function which signature is (args..., err).

Why

Most async functions of nodejs framework have this signature (nodejs fs, or databases such mongo or NeDB).

When you start coding some async stuff without Promises (because the function does not handle it and just has a callback as argument), you end up with a lot of callbacks inside others callbacks, a lot of if to handle the arguments (err, result), a lot of repetitions, a lot of indentations, and the code is just ugly, let's face it.

Promisifying those async functions allows you to think more easily with the processes that's going on and generally to have a cleaner code, way more logic when you read it (you don't need to have to think about contexts embedded into others contexts, then what-if else etc.).

Just check the examples below if you're not sure that fits your needs.

Syntax

It returns a function that encapsule the given function into a Promise.

const read = waitForMe(fs.readFile);
const insert = waitForMe(db.insert, db); // context (`this`) is needed sometimes

then/catch

You can use it with with classic then/catch syntax, eg:

import waitForMe from 'wait-for-me';
const read = waitForMe(fs.readFile);

read('/etc/passwd')
  .then(data => sendEmail(data))
  .catch(err => console.error(`Couldn't access /etc/passwd: ${err.message}`))

async/await

Or you can use it with the async/await keywords (you need Babel for now to handle them), eg:

import fs from 'fs';
import waitForMe from 'wait-for-me';
const read = waitForMe(fs.readFile);

try {
  const data = await read('/etc/passwd')
  sendEmail(data);
} catch(err) {
  console.error(`Couldn't access passwd: ${err.message}`);
}

Generally, the async/await notation is cleaner, especially if you have several of them:

...
const get = waitForMe(request.get, request);
const tweet = waitForMe(client.post, client);

try {
  // get the content of an image
  // then post the image to twitter
  // then post a tweet using the image

  const result = await get({ url: src, encoding: null });
  const media = await tweet('media/upload', { media: result.body });
  const t = await tweet('statuses/update', { status: 'Yep', media_ids: media.media_id_string });
  console.log('Tweet OK!');
} catch(error) {
  console.error(error);
}

All those functions are async but it's like there are not!

Promise

The project does not include any polyfill for Promise. If you are using a plain nodejs process, you won't need it, it's handled natively.

Origin

I started a project with some callbacks hell, then wanted to promisify some functions because it was ugly. Few days before, I saw a conversation about this topic, involving @RReverser who suggests this approach.

I really liked it, very clean code. I just added the context option for me but all credits go to him.

You can find other projects promisifying function but I found them more complicated, doing more stuff, having polyfill or other thing I didn't want.

I just wanted to promisify some function I pock, in a nodejs process, that's it.