# Code coverage report for mz/_promisify.js

## Statements: 100% (14 / 14) Branches: 100% (4 / 4) Functions: 100% (3 / 3) Lines: 100% (14 / 14) Ignored: none

All files » mz/ » _promisify.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
    1

    1
    45

    1
    10
    10
    3

    7
    7
    1
    1
    2

    1

    6

    var Promise = require('native-or-bluebird')

    module.exports = function mz_promisify(name, fn) {
      return eval('(function ' + name + '() {\n'
        + 'var len = arguments.length\n'
        + 'var args = new Array(len + 1)\n'
        + 'for (var i = 0; i < len; ++i) args[i] = arguments[i]\n'
        + 'var lastIndex = i\n'
        + 'return new Promise(function (resolve, reject) {\n'
          + 'args[lastIndex] = makeCallback(resolve, reject)\n'
          + 'fn.apply(null, args)\n'
        + '})\n'
      + '})')
    }

    function makeCallback(resolve, reject) {
      return function(err, value) {
        if (err) {
          reject(err)
        } else {
          var len = arguments.length
          if (len > 2) {
            var values = new Array(len - 1)
            for (var i = 1; i < len; ++i) {
              values[i - 1] = arguments[i]
            }
            resolve(values)
          } else {
            resolve(value)
          }
        }
      }
    }