Skip to content

Latest commit

 

History

History
82 lines (60 loc) · 2.58 KB

README.md

File metadata and controls

82 lines (60 loc) · 2.58 KB

interject

Build Status Coverage Status NPM version Davis Dependency Status

browser support

Do something as an aside to a callback.

Potentially useful for logging calls to particular callbacks. Maybe other stuff too.

example

var interject = require('interject');

var say = function (msg, cb) {
    console.log(msg);
    if (cb) cb(null, msg);
};

var afterSay = function (err, msg) {
    console.log('You said: ' + msg +'\n');
};

var logMessage = function (err, msg) {
    console.log('On ' + (new Date()) + ', somebody said: ' + msg);
};

say('Interject me please.', afterSay);
// Output:
// Interject me please.
// You said: Interject me please.

var sayLogged = interject(say, logMessage);
sayLogged('Interject me please.', afterSay);
// Output:
// Interject me please.
// On Fri Mar 28 2014 18:20:41 GMT-0400 (EDT), somebody said: Interject me please.
// You said: Interject me please.

api

var interjected = interject(func, function interjection () {})

interjected(function callback () {
    /* do something */
});

Assuming the implementation of func accepts a callback (as it's last parameter), the function returned by interject will invoke both the interjection (as an aside) and the callback provided to interjected at the time the func implementation would have executed it's callback. It's important to note that the interjection will be called just before the callback, but it is not gauranteed to complete before callback is called.

The interjection receives the same arguments as the callback.

Also of note - when nesting interjections, the interjections will be called in the order they are defined before the final callback. Example:

var original = function (cb) { cb() };
var interjection1 = function () { /* I am called 1st */ };
var interjected = interject(original, interjection1);
var interjection2 = function () { /* I am called 2nd */ };
var interjected = interject(interjected, interjection2);
interjected();

install

npm install interject

license

MIT