Skip to content

node.js module that brings some syntactic sugar for calling a function within setImmediate

License

Notifications You must be signed in to change notification settings

hernanhht/immediate-invocation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Immediate

Small module that ease the invocation of a function within a setImmediate call. Following the best practice of always calling callbacks in an async fashion, this library will add some syntactic sugar (a little implementation of a Cristian Douce idea).

Installation

npm i immediate-invocation

Classic invocation of a callback

A typical node.js code looks like this:

function addNumber(first, second, cb) {
  if (first === null) {
    return cb(new Error('first argument is mandatory'));
  }
  
  cb(null, first + second);
}

This has the drawback that breaks the implicit contract of executing the callback in the next tick. To avoid this we can wrap the cb call with setImmediate:

Invoking a callback with setImmediate

function addNumber(first, second, cb) {
  if (first === null) {
    return setImmediate(function () {
      cb(new Error('first argument is mandatory'));
    });
  }

  return setImmediate(function () {
    cb(null, first + second);
  });
}

This is a bit verbose. Now an example using this module:

Invoking a callback with this module (immediate)

var immediate = require('immediate');

function addNumber(first, second, cb) {
  if (first === null) {
    return immediate(cb, new Error('first argument is mandatory'));
  }
  
  immediate(cb, null, first + second);
}

About

node.js module that brings some syntactic sugar for calling a function within setImmediate

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published