arco-design / arco-design

A comprehensive React UI components library based on Arco Design
https://arco.design
MIT License
4.74k stars 631 forks source link

Form validate return eternal pending promise while pass `undefined` as a parameter #1316

Open ExcitedSpider opened 1 year ago

ExcitedSpider commented 1 year ago

Basic Info

Steps to reproduce

  1. Write a form item rule validator which return a Promise
  2. Use form.validate()
ExcitedSpider commented 1 year ago

I reckon this issue is related to the implementation of components/Form/promisify.ts which implemented first default function parameter wrongly.

function promisify<T = any>(fn: (...args: any[]) => any): () => Promise<T> {
  return Object.defineProperty(
    function (...args: any[]) {
      if (typeof args[args.length - 1] === 'function') fn.apply(this, args);
      else {
        return new Promise((resolve, reject) => {
          args[args.length] = (err, res) => {
            if (err) return reject(new ValidateError(err));
            resolve(res);
          };
          args.length++;
          fn.apply(this, args);
        });
      }
    },
    'name',
    { value: fn.name }
  );
}