简体   繁体   English

返回Promises数组的函数的类型应该是什么

[英]What should be the type of function which returns array of Promises

I have a function, which basically returns array of Promises. 我有一个函数,它基本上返回Promises数组。 What should be the return type of the function? 函数的返回类型应该是什么? Below is the code 下面是代码

const makeLabelNodesRequest: Promise<K8sResourceKind> = (selectedNode: NodeKind[]) => {
const labelPath = '/metadata/labels';
const labelData = selectedNode.map((node: NodeKind) => {
  const labels = SelectorInput.arrayify(_.get(node, labelPath.split('/').slice(1)));
  const lblVal = { ...SelectorInput.objectify(labels), ...labelObj };
  const patch = [
    {
      op: labels.length ? 'replace' : 'add',
      value: lblVal,
      path: labelPath,
    },
  ];
  return k8sPatch(NodeModel, node, patch); // returns a promise
});
return labelData; //array of Promise

}; };

Its giving this error - [ts] Type '(selectedNode: NodeKind[]) => Promise[]' is missing the following properties from type 'Promise': then, catch, [Symbol.toStringTag]. 它给出此错误-[ts]类型'(selectedNode:NodeKind [])=> Promise []'缺少类型'Promise'的以下属性:然后,捕获[Symbol.toStringTag]。 Please note NodeKind and K8sResourceKind are custom types. 请注意,NodeKind和K8sResourceKind是自定义类型。

You have 2 options : 您有2个选择:

const makeLabelNodesRequest: Array<Promise<K8sResourceKind>>

Or 要么

const makeLabelNodesRequest: Promise<K8sResourceKind>[]

The first solution is just an alias of the second one, which should be indicated by your IDE autocompletion. 第一个解决方案只是第二个解决方案的别名,应该由您的IDE自动完成功能指示出来。

None of these solutions have an advantage other the other, just stay consistent and use a single syntax in your codebase. 这些解决方案没有一个具有其他优势,只是保持一致并在您的代码库中使用单一语法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM