Giter Club home page Giter Club logo

quin's Introduction

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();

quin's People

Contributors

midknight41 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.