promise-useful-utils
Version:
useful functions for working with promises
39 lines (33 loc) • 874 B
JavaScript
/**
* @param {function} fn
* @param {(Array.<*>|*)} args
* @param {*} ctx
* @return {Promise}
*/
;
var _Promise = require("babel-runtime/core-js/promise")["default"];
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.method = method;
module.exports["try"] = function (fn, args, ctx) {
return _Promise.resolve().then(function () {
return Array.isArray(args) ? fn.apply(ctx, args) : fn.call(ctx, args);
});
};
/**
* @param {function} method
* @return {function}
*/
function method(fn) {
// use rest for babel, otherwise arguments will slow
return function () {
var _this = this;
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return _Promise.resolve().then(function () {
return fn.apply(_this, args);
});
};
}