arco-design icon indicating copy to clipboard operation
arco-design copied to clipboard

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

Open ExcitedSpider opened this issue 2 years ago • 1 comments

Basic Info

  • Package Name And Version: @arco-design/[email protected]
  • Browser: chrome104.0.0.0
  • Reproduction Link: https://codesandbox.io/s/sad-shape-rjyq8b?file=/demo.js

Steps to reproduce

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

ExcitedSpider avatar Aug 22 '22 09:08 ExcitedSpider

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 }
  );
}

ExcitedSpider avatar Aug 22 '22 09:08 ExcitedSpider