Skip to content

Commit

Permalink
[Refactor] use callBound helper from es-abstract for robustness
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Dec 19, 2019
1 parent c3043e6 commit baa0cf6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
16 changes: 9 additions & 7 deletions implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ if (typeof Promise !== 'function') {
throw new TypeError('`Promise` must be globally available for util.promisify to work.');
}

var slice = Function.call.bind(Array.prototype.slice);
var concat = Function.call.bind(Array.prototype.concat);
var forEach = Function.call.bind(Array.prototype.forEach);
var callBound = require('es-abstract/helpers/callBound');

var $slice = callBound('Array.prototype.slice');
var $concat = callBound('Array.prototype.concat');
var $forEach = callBound('Array.prototype.forEach');

var hasSymbols = require('has-symbols')();

Expand Down Expand Up @@ -51,16 +53,16 @@ module.exports = function promisify(orig) {
var argumentNames = orig[kCustomPromisifyArgsSymbol];

var promisified = function fn() {
var args = slice(arguments);
var args = $slice(arguments);
var self = this; // eslint-disable-line no-invalid-this
return new Promise(function (resolve, reject) {
orig.apply(self, concat(args, function (err) {
var values = arguments.length > 1 ? slice(arguments, 1) : [];
orig.apply(self, $concat(args, function (err) {
var values = arguments.length > 1 ? $slice(arguments, 1) : [];
if (err) {
reject(err);
} else if (typeof argumentNames !== 'undefined' && values.length > 1) {
var obj = {};
forEach(argumentNames, function (name, index) {
$forEach(argumentNames, function (name, index) {
obj[name] = values[index];
});
resolve(obj);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"main": "index.js",
"dependencies": {
"define-properties": "^1.1.3",
"es-abstract": "^1.17.0-next.1",
"has-symbols": "^1.0.1",
"object.getownpropertydescriptors": "^2.1.0"
},
Expand Down

0 comments on commit baa0cf6

Please sign in to comment.