/*
 * @Author: codingfly
 * @Description:
 * @Date: 2021-08-04 14:54:17
 * @LastEditTime: 2021-08-26 15:12:45
 * @FilePath: \young\src\_utils\sync.ts
 */
var sync = function sync(fn) {
  for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
    args[_key - 1] = arguments[_key];
  }

  return new Promise(function (resolve, reject) {
    var obj = Object.assign({}, args[0]);
    var success = obj.success;

    obj.success = function () {
      for (var _len2 = arguments.length, params = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
        params[_key2] = arguments[_key2];
      }

      success && success.apply.apply(success, [this].concat(params));
      var r = params.length > 1 ? params : params[0];
      resolve(r);
    };

    var fail = obj.fail;

    obj.fail = function () {
      for (var _len3 = arguments.length, params = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
        params[_key3] = arguments[_key3];
      }

      fail && fail.apply.apply(fail, [this].concat(params));
      var r = params.length > 1 ? params : params[0];
      reject(r);
    };

    fn(obj);
  });
};

export default sync;