New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

double-done

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

double-done

Double Done design pattern

  • 0.0.2
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

double-done

Build Status Code Climate Test Coverage Donate

NPM version dependency status dev dependency status

Table of contents

Preface

I have always liked JavaScript, but when I deeply understood its power, I have fallen in love with it!

One of the greatest aspect of JavaScript is that, thanks to its other strenght points, it helps saving the most expensive resource of the whole software life cycle: developers time.

ABSTRACT

Single Done desing pattern

From now on: SD

In my experience I found that so often I have to write an async function which has to call some other async functions to achieve its ojective. Many of the functions I wrote look like:

function myAsyncFunctionSD(param1, done) {
  otherAsyncFunction1(function(err, param2) {
    if(err)
      return done(err);

    otherAsyncFunction2(param1, param2, function(err, param3) {
      if(err)
        return done(err);

      otherAsyncFunction3(param1, param3, function(err, param4) {
        if(err)
          return done(err);

        done(null, param2, param4);
      });
    });
  });
}

After written some functions I found that continuously repeated block

  if(err)
    return done(err);

was just a time waster, both when writing and reading the code.

Hard Double Done desing pattern

From now on: HDD

An environment where async functions accept two done functions, one called in case of error and one called in case of success could help saving that time. My same async function would appear so:

function myAsyncFunctionHDD(param1, doneErr, doneOk) {
  otherAsyncFunction1HDD(doneErr, function(param2) {
    otherAsyncFunction2HDD(param1, param2, doneErr, function(param3) {
      otherAsyncFunction3HDD(param1, param3, doneErr, function(param4) {
        doneOk(param2, param4);
      });
    });
  });
}

Soft Double Done desing pattern

or simply: Double Done design pattern

From now on: SDD or simply: DD

I immediately realized that changing myAsyncFunctionSD in myAsyncFunctionHDD would be a breaking change. More than this I had to considerate that not always calling an HDD async function is more confortable than calling a SD async function (i.e.: when we have to do something both in cases of error or success).

For these two reasons I decided to write this package which may help us writing DD async funciotns.

A DD async funciotn should be documentete as follows:

 # myAsyncFunctionDD(param1, done[, doneOk])

in case of error, done will be called with the error as first parameter; in case of success, if the caller have not passed a doneOk function, (as in SD design pattern) done function will be called with null as first parameter followed by other parameters (which are the myAsyncFunctionDD result), otherwise doneOk function will be called passing to it only the parameters which are the myAsyncFunctionDD result.

Back to: top - Table of contents

API definition

require('doble-done');

Returns the double-done utility function.

dd(done, doneOk)

Returns the doneOk function which must be called in case of success regardless if the caller passed it or not.

function myAsyncFunctionDD(param1, done, doneOk) {
  doneOk = dd(done, doneOk);

From now on done and doneOk are the functions to call respectively in case of error or success.

doneOk.dd(callbackOk)

Lets us to call an SD async function writing only the callbackOk function, if the called async function gets an error, doneOk.dd makes it calling the original done function.

function myAsyncFunctionDD(param1, done, doneOk) {
  doneOk = dd(done, doneOk);

  otherAsyncFunction1SD(doneOk.dd(function(param2) {
    // if otherAsyncFunction1SD gets an error, done will be called
    // otherwise this function is executed 
  }));

Back to: top - Table of contents

Licence

MIT Licence

Bugs

Do not hesitate to report any bug or consideration @github.

ChangeLog

  • 2017-01-05 - v0.0.2
    • Cosmetics
  • 2017-01-05 - v0.0.1
    • First release
  • 2016-12-30 - v0.0.0
    • First idea

Keywords

FAQs

Package last updated on 05 Jan 2017

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc