Code coverage report for promise/lib/node-extensions.js

Statements: 94.74% (36 / 38)      Branches: 90% (18 / 20)      Functions: 92.31% (12 / 13)      Lines: 94.44% (34 / 36)      Ignored: none     

All files » promise/lib/ » node-extensions.js
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 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74          1 1   1       1 4 4 4 4 4 4 1   4 3 2   4 4             1         1 5 5 5   5 5 5   1         1 1             1 15   13 11 11     2 2        
'use strict';
 
// This file contains then/promise specific extensions that are only useful
// for node.js interop
 
var Promise = require('./core.js');
var asap = require('asap');
 
module.exports = Promise;
 
/* Static Functions */
 
Promise.denodeify = function (fn, argumentCount) {
  argumentCount = argumentCount || Infinity;
  return function () {
    var self = this;
    var args = Array.prototype.slice.call(arguments);
    return new Promise(function (resolve, reject) {
      while (args.length && args.length > argumentCount) {
        args.pop();
      }
      args.push(function (err, res) {
        if (err) reject(err);
        else resolve(res);
      })
      var res = fn.apply(self, args);
      if (res &&
        (
          typeof res === 'object' ||
          typeof res === 'function'
        ) &&
        typeof res.then === 'function'
      ) {
        resolve(res);
      }
    })
  }
}
Promise.nodeify = function (fn) {
  return function () {
    var args = Array.prototype.slice.call(arguments);
    var callback =
      typeof args[args.length - 1] === 'function' ? args.pop() : null;
    var ctx = this;
    try {
      return fn.apply(this, arguments).nodeify(callback, ctx);
    } catch (ex) {
      Iif (callback === null || typeof callback == 'undefined') {
        return new Promise(function (resolve, reject) {
          reject(ex);
        });
      } else {
        asap(function () {
          callback.call(ctx, ex);
        })
      }
    }
  }
}
 
Promise.prototype.nodeify = function (callback, ctx) {
  if (typeof callback != 'function') return this;
 
  this.then(function (value) {
    asap(function () {
      callback.call(ctx, null, value);
    });
  }, function (err) {
    asap(function () {
      callback.call(ctx, err);
    });
  });
}