all files / lib/mixins/ promise.js

100% Statements 16/16
88.89% Branches 8/9
100% Functions 5/5
100% Lines 16/16
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32      107× 107×   107×       107×     29× 29×   29× 159× 66×       29×      
'use strict';
 
var _ = require('lodash');
 
var wrapper = function () {
  var result = this._super.apply(this, arguments);
  var callback = arguments[arguments.length - 1];
 
  if(typeof result !== 'undefined' && _.isFunction(result.then) && _.isFunction(callback)) {
    result.then(function(data) {
      callback(null, data);
    }, function(error) {
      callback(error);
    });
  }
  return result;
};
 
module.exports = function (service) {
  Eif (typeof service.mixin === 'function') {
    var mixin = {};
 
    _.each(this.methods, function(method) {
      if(typeof service[method] === 'function') {
        mixin[method] = wrapper;
      }
    });
 
    service.mixin(mixin);
  }
};