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

promise-ring

v0.1.0

Published

Convenience methods for converting Node.js callbacks into native Promises.

Downloads

20

Readme

promise-ring

Convenience methods for converting Node.js callbacks into native Promises.

npm version GitHub tag Build status Coverage License

Install

npm install promise-ring --save

Overview

promise-ring is small, simple library with no dependencies that eases the use of native JavaScript Promises in projects without a Promise library.

Important: promise-ring requires a native Promise implementation. If Promise is not defined, it throws immediately. Therefore, projects that need to support older environments (like Node.js 0.10.x) should look for similar functionality from Q, Bluebird, promise, etc..

For more about using Promise to write better code, see:

API / Examples

Each of the following snippets comes from the example directory in the repository and assumes the following requires:

var fs = require("fs");

var Database = require("./database.js");
var db = new Database("MockDB");

var pr = require("promise-ring");

call

call wraps a single invocation of a callback-based method and returns a Promise. If the first (i.e., "error") parameter of the callback is not null, the Promise is rejected. Otherwise, the Promise is resolved with the second parameter of the callback. If the callback received multiple parameters, the Promise is resolved with an Array of them.

pr.call(fs.readFile, "../package.json", { "encoding": "utf8" })
  .then(function(data) {
    console.log(JSON.parse(data).name);
  });

apply

apply is to call as Function.apply is to Function.call.

pr.apply(fs.readFile, [ "../package.json", { "encoding": "utf8" } ])
  .then(function(data) {
    console.log(JSON.parse(data).name);
  });

callBound

callBound is like call but allows the thisArg to be specified for scenarios that require it (like class instances).

pr.callBound(db, db.query, "promise-ring")
  .then(function(result) {
    console.log(result);
  });

applyBound

applyBound is to callBound as apply is to call.

pr.applyBound(db, db.query, [ "promise-ring" ])
  .then(function(result) {
    console.log(result);
  });

wrap

Instead of using call multiple times for the same function, wrap creates a wrapper function that can be used multiple times wherever it is needed.

var readFile = pr.wrap(fs.readFile);

readFile("../package.json", { "encoding": "utf8" })
  .then(function(data) {
    console.log(JSON.parse(data).name);
  });

wrapBound

wrapBound is to wrap as callBound is to call.

var dbquery = pr.wrapBound(db, db.query);

dbquery("promise-ring")
  .then(function(result) {
    console.log(result);
  });

wrapAll

As a convenience, wrapAll creates Promise wrappers for all functions on an object and binds thisArg to that object instance (similar to the *Bound functions above).

Note: wrapAll is not smart and may include functions that don't take a callback - it's up to the author to ensure only appropriate wrappers are used.

var fsw = pr.wrapAll(fs);
var dbw = pr.wrapAll(db);

fsw.readFile("../package.json", { "encoding": "utf8" })
  .then(function(data) {
    console.log(JSON.parse(data).name);
  });

dbw.query("promise-ring")
  .then(function(result) {
    console.log(result);
  });

History

  • 0.1.0 - Initial release.