Javascript 如何为流注释此函数

Javascript 如何为流注释此函数,javascript,node.js,ecmascript-6,flowtype,Javascript,Node.js,Ecmascript 6,Flowtype,我在使用Flow()的文件中有这个JS函数 我将如何对此进行注释?如上所述,这将是我建议的解决方案: class Foo { // We are using generics <U, T> to define the input type / output type (may be the same... I don't know the use-cases) static promiseWrapper<U, T>(fn: (...args: Array<U

我在使用Flow()的文件中有这个JS函数

我将如何对此进行注释?

如上所述,这将是我建议的解决方案:

class Foo {
  // We are using generics <U, T> to define the input type / output type (may be the same... I don't know the use-cases)
  static promiseWrapper<U, T>(fn: (...args: Array<U>) => Promise<T>, ...args: Array<U>): Promise<T> {
    return new Promise(async (resolve, reject) => {
      try {
        // I felt like handling the return value of this await function
        const ret = await fn(...args);
        resolve(ret);
      } catch (e) {
        reject(e);
      }
    });
  }
}


// Most of the type inference comes from this function
function fun(...args: Array<number>): Promise<string> {
  const ret = args.reduce((result, num) => (result + num), 0);
  return Promise.resolve(ret.toString());
}

const myProm = Foo.promiseWrapper(fun, 1, 2, 3);

// Here, total should be inferred as string, since our `fun` function returns a Promise<string>
// Generic function definition <T> picks this up correctly :-)
myProm.then((total) => {
  // $ExpectError : total should be inferred as string, hence it should fail on numerical addition
  const foo: number = total + 1;

});
class-Foo{
//我们使用泛型来定义输入类型/输出类型(可能是相同的…我不知道用例)
静态promiseWrapper(fn:(…args:Array)=>Promise,…args:Array):Promise{
返回新承诺(异步(解析、拒绝)=>{
试一试{
//我想处理这个等待函数的返回值
const ret=等待fn(…args);
解决(ret);
}捕获(e){
拒绝(e);
}
});
}
}
//大多数类型推断都来自此函数
函数乐趣(…参数:数组):承诺{
const ret=args.reduce((result,num)=>(result+num),0);
返回Promise.resolve(ret.toString());
}
const myProm=Foo.promiseWrapper(fun,1,2,3);
//这里,total应该被推断为string,因为我们的'fun'函数返回一个承诺
//泛型函数定义正确地选择了它:-)
myProm.then((总数)=>{
//$ExpectError:total应推断为字符串,因此在数字相加时应失败
常量foo:number=total+1;
});

这并不是使用异步函数的方式。使用这种逻辑,
promiseWrapper
将是异步函数,您只需执行
static async promiseWrapper(fn,…args){wait fn(…args);}
我在上面提到过,但这里也要提到,
new Promise
的这种用法是一种反模式
promiseWrapper
应该是异步函数,它应该只返回结果,而不是调用
resolve
回调。
class Foo {
  // We are using generics <U, T> to define the input type / output type (may be the same... I don't know the use-cases)
  static promiseWrapper<U, T>(fn: (...args: Array<U>) => Promise<T>, ...args: Array<U>): Promise<T> {
    return new Promise(async (resolve, reject) => {
      try {
        // I felt like handling the return value of this await function
        const ret = await fn(...args);
        resolve(ret);
      } catch (e) {
        reject(e);
      }
    });
  }
}


// Most of the type inference comes from this function
function fun(...args: Array<number>): Promise<string> {
  const ret = args.reduce((result, num) => (result + num), 0);
  return Promise.resolve(ret.toString());
}

const myProm = Foo.promiseWrapper(fun, 1, 2, 3);

// Here, total should be inferred as string, since our `fun` function returns a Promise<string>
// Generic function definition <T> picks this up correctly :-)
myProm.then((total) => {
  // $ExpectError : total should be inferred as string, hence it should fail on numerical addition
  const foo: number = total + 1;

});