| 1 |
// Promisify asynchronous function |
| 2 |
|
| 3 |
"use strict"; |
| 4 |
|
| 5 |
var isValue = require("es5-ext/object/is-value") |
| 6 |
, callable = require("es5-ext/object/valid-callable") |
| 7 |
, toNaturalNumber = require("es5-ext/number/to-pos-integer") |
| 8 |
, callAsync = require("./call-async")._base; |
| 9 |
|
| 10 |
module.exports = function (length) { |
| 11 |
var fn, result; |
| 12 |
fn = callable(this); |
| 13 |
if (fn.returnsPromise) return fn; |
| 14 |
if (isValue(length)) length = toNaturalNumber(length); |
| 15 |
result = function () { return callAsync(fn, length, this, arguments); }; |
| 16 |
result.returnsPromise = true; |
| 17 |
return result; |
| 18 |
}; |