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

quin

v0.0.2

Published

Dependency injector for Q Promise chains

Downloads

4

Readme

quin

quin is a dependency injector to make code for Q Promise chains a little cleaner.

Assume you have a scenario where some (but not all) steps in your promises chain require some sort additional dependency. Depending how you solve this, you can get promises that are very tightly coupled to each other or lots of inline functions.

quin looks to solve this by allowing you to inject dependencies into your promises chain in a fairly transparent manner.

npm install quin

Example

Assume we have a messageService that we use to talk to a message queue. We want to retrieve a message, validate and delete it.

First, let's build some simple callbacks that's we'll make into promises:

//needs the messageService so we pass it in at the start of the chain
function getIt(messageService, cb) {
  messageService.getMsg(function (err, msg) {
    cb(err, msg);
  });
}

//this function does not know or care about the messageService
function validateIt(msg, cb) {

  if (msg.isGood == false) {
    cb(new Error("INVALID"), null);
  }

  cb(null, msg);
}

//this function needs the messageService to do it's work
function deleteIt(messageService, msg, cb) {

  messageService.deleteMsg(msg, function () {
    cb(null, msg);
  });
}

But we'd rather use promises, so let's make some and let quin sort out the dependency:

var Q = require("q");
var quin = require("quin");
var messageService = require("./whatever");

//plain old promises
var getMessage = Q.denodeify(getIt);
var validateMessage = Q.denodeify(validateIt);
var del = Q.denodeify(deleteIt);

//quin returns a promise proxy injecting the extra dependency
var deleteMessage = quin.inject(del, messageService);

//Now execute the promise chain
getMessage(messageService)
  .then(validateMessage)
  .then(deleteMessage)
  .done();

The purpose and the flow of the chain is easier to read now and the validateMessage promise is not tightly coupled to the preceeding promise.

If you are actually starting from callbacks (instead of promises) the denodeify method is available on quin for convenience.

var quin = require("quin");
var messageService = require("./whatever");

//get your promises
var getMessage = quin.denodeify(getIt);
var validateMessage = quin.denodeify(validateIt);
var deleteMessage = quin.denodeify(deleteIt, messageService);

//Now execute the promise chain
getMessage(messageService)
  .then(validateMessage)
  .then(deleteMessage)
  .done();