Skip to content

wejs/simple-hooks-callback

Repository files navigation

simple-hooks-callback

NPM version Build Status Code Climate Coverage Status

Hooks with callback, works like events but with async callbacks and in order

The easiest way to run asynchronous event functions with callback and execution order. Works like javascript events and is small and dont have dependencies

Installation

npm install --save simple-hooks-callback

Usage

// get hooks class
var Hooks = require('simple-hooks-callback');
// start new hook instance with new hooks list
var hooks = new Hooks();

// your function
function function1 (data, cb){
  // do something ...
  console.log('function1', data);

  // then remember to call the callback
  cb();
  // or run **return cb(err);** if you want return error
}

function function2 (data, cb){
  // do something ...
  console.log('function2', data);
  
  // timeout to simulate the async request
  setTimeout(function(){ 
    // then remember to call the callback
    cb();
  }, 1000);
}

// register one function to run on hook event
hooks.on('do-something', function1);
// register other function
hooks.on('do-something', function2);

// run all registered functions
hooks.trigger('do-something', { someData: 'someValue' }, function AfterAll (err){
  if (err) throw err;

  console.log('doneAll');
  // all done
});

// un register the function1
hooks.off('do-something', function1);

Run this example in your browser

Link: https://tonicdev.com/570d0017d5cdb01100394e1a/570d0017d5cdb01100394e1b

License

MIT © Alberto Souza

About

Hooks with callback, works like events but with callbacks

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published