🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

deferred-chain

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

deferred-chain

A system of abusing callbacks

latest
Source
npmnpm
Version
0.1.0
Version published
Weekly downloads
6
200%
Maintainers
1
Weekly downloads
 
Created
Source

Deferrable

A system for abusing callbacks.

Background

An ordinary Node-style long-running function looks something like this:

doSomethingLongRunning (..., cb) {
  ...
}

When that process is done, cb is fired off, with an error and a data parameter. However, this often means we end up with spaghetti code that looks something like this:

Group.fromId(id, function (err, group) {
  if (err)
    return dealWithError(err);

  group.getOwner(function (err, user) {
    if (err)
      return dealWithError(err);

    user.getGroups(function (err, groups) {
      if (err)
        return dealWithError(err);

      // do something with the groups
    });
  })
});

It would be much nicer if we could get to that end goal without all the middle-men callbacks getting in the way, like this:

Group.fromId(id).getOwner().getGroups(function (err, groups) {
  if (err)
    return dealWithError(err);

  // do something with the groups
});

Usage

We can achieve this by marking our methods as deferrable, and providing a prototype for the expected return value:

const deferrable = require('deferrable');

Group.fromId = deferrable(function(id, cb) {
  db.find(..., cb);
}, User.prototype);

Keywords

callbacks

FAQs

Package last updated on 17 Jan 2014

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts