Promisification

字如其名,把你变成Promise。

其在实际开发中的应用数不胜数,许多函数和库都是基于Callback的。但是Promise明显更加方便,结合async, await大大简化了代码。

为了更好的理解,让我们看下面的例子:

function loadScript(src, callback) {
  let script = document.createElement('script');
  script.src = src;

  script.onload = () => callback(null, script);
  script.onerror = () => callback(new Error(`Script load error for ${src}`));

  document.head.append(script);
}

// usage:
loadScript('path/script.js', (err, script) => {...})

这是一个典型的基于Callback装载js脚本的方法,至于使用回调的drawback应该大家都耳熟能详,这里不过多赘述了。

好了,让我们Promisify它。

let loadScriptPromise = function(src) {
  return new Promise((resolve, reject) => {
    loadScript(src, (err, script) => { // the same as before
      if (err) reject(err);
      else resolve(script);
    });
  });
};

// usage:
loadScriptPromise('path/script.js').then(...)

这里同样加载脚本,不同的是这里返回了一个Promise,参数只需要传入src,当脚本成功加载,resolve这个Promise,加载失败reject这个Promise。

但是实际的使用场景当中,我们不仅仅只promisify一个函数,我们需要一个装饰器来把它写的通用一些。

function promisify(f) {
  return function (...args) { // return a wrapper-function 
    return new Promise((resolve, reject) => {
      function callback(err, result) { // our custom callback for f 
        if (err) {
          reject(err);
        } else {
          resolve(result);
        }
      }

      args.push(callback); // append our custom callback to the end of f arguments

      f.call(this, ...args); // call the original function
    });
  };
}

// usage:
let loadScriptPromise = promisify(loadScript);
loadScriptPromise(src).then(...);

可能显得稍微有点复杂,这地方调用promisify返回一个包裹函数,这个包裹函数返回一个Promise,随后将自定义的Callback添加到args中,调用promisify传进来的函数。这样就可以promisify任何的函数了。

如果原始的f函数希望回调函数可以支持多个参数呢?形如callback(err, res1, res2, ...)?

升级一下装饰器,让他支持多个参数,这里增加一个参数,区分是否多参数。

// promisify(f, true) to get array of results
function promisify(f, manyArgs = false) {
  return function (...args) {
    return new Promise((resolve, reject) => {
      function callback(err, ...results) { // our custom callback for f
        if (err) {
          reject(err);
        } else {
          // resolve with all callback results if manyArgs is specified
          resolve(manyArgs ? results : results[0]);
        }
      }

      args.push(callback);

      f.call(this, ...args);
    });
  };
}

// usage:
f = promisify(f, true);
f(...).then(arrayOfResults => ..., err => ...);

这样加一个参数,决定回调函数是否是多参数。

如果是定制化的Callback,类似不需要err的,可以手动promisify一下,无需装饰器。

在node中,有一个内置的util.promisify函数是特定的实现。

这里需要注意一下,promise只有一个结果,但是回调从技术层面可以被调用多次。

所以这里promisifycation只适用于回调函数调用一次的情形,更多的调用会被忽略。

附上es6-promisify源码

// Symbols is a better way to do this, but not all browsers have good support,
// so instead we'll just make do with a very unlikely string.
const customArgumentsToken = "__ES6-PROMISIFY--CUSTOM-ARGUMENTS__";

/**
 * promisify()
 * Transforms callback-based function -- func(arg1, arg2 .. argN, callback) --
 * into an ES6-compatible Promise. Promisify provides a default callback of the
 * form (error, result) and rejects when `error` is truthy.
 *
 * @param {function} original - The function to promisify
 * @return {function} A promisified version of `original`
 */
export function promisify(original) {
    // Ensure the argument is a function
    if (typeof original !== "function") {
        throw new TypeError("Argument to promisify must be a function");
    }

    // If the user has asked us to decode argument names for them, honour that
    const argumentNames = original[customArgumentsToken];

    // If the user has supplied a custom Promise implementation, use it.
    // Otherwise fall back to whatever we can find on the global object.
    const ES6Promise = promisify.Promise || Promise;

    // If we can find no Promise implemention, then fail now.
    if (typeof ES6Promise !== "function") {
        throw new Error("No Promise implementation found; do you need a polyfill?");
    }

    return function (...args) {
        return new ES6Promise((resolve, reject) => {
            // Append the callback bound to the context
            args.push(function callback(err, ...values) {
                if (err) {
                    return reject(err);
                }

                if (values.length === 1 || !argumentNames) {
                    return resolve(values[0]);
                }

                const o = {};
                values.forEach((value, index) => {
                    const name = argumentNames[index];
                    if (name) {
                        o[name] = value;
                    }
                });

                resolve(o);
            });

            // Call the function.
            original.apply(this, args);
        });
    };
}

// Attach this symbol to the exported function, so users can use it
promisify.argumentNames = customArgumentsToken;
promisify.Promise = undefined;

--end--

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值