Browse Source

[promisify]: simplify via es6 syntax

mightyplow 7 years ago
parent
commit
97e3c06ced
1 changed files with 4 additions and 16 deletions
  1. 4 16
      src/function/promisify.js

+ 4 - 16
src/function/promisify.js

@@ -3,26 +3,14 @@
  * @param fn
  * @param context
  */
-// cannot use fat arrow function since we don't get the arguments, then
-const promisify = (fn, context = null) => function () {
-
-    // copy passed arguments to a new array
-    const args = toArray(arguments);
-
+const promisify = (fn, context = null) => (...args) => {
     return new Promise((resolve, reject) => {
-        // add a callback to the arguments, which resolves the promise with the result
-        args.push((err, data) => {
-            if (err) {
-                reject(err);
-            } else {
-                resolve(data);
-            }
-        });
+        // create a callback to handle error and data
+        const callback = (err, data) => err ? reject(err) : resolve(data);
 
         // call the original function with our callback flavoured arguments
-        fn.apply(context, args);
+        fn.call(context, ...args, callback);
     });
 };
 
-
 export default promisify;