Skip to content

How return count result instead closure function?

I have a function that returns boolean if tree has at least one enebled value:

  treeHasEnabledNode(): Function {
    let enabled = false;

    return function isNodeEnabled(node: T): boolean {
      if (!node || !node.children || !node.children.length) return enabled;
      if (node.enabled && node.enabled !== undefined) return true;

      node.children.forEach((node: T) => {
        enabled = isNodeEnabled(node);

      return enabled;

Using is:

let hasEnabled = treeHasEnabledNode()(this.tree);

How to return result not calling outher functon (this.tree)?


You can go about this a few ways. The most simple one would probably be to invoke the internal function within the outer function, and return the result:

function treeHasEnabledNode(node) {
  let enabled = false;

  function isNodeEnabled(node) {
    // do whatever. for example:
    return enabled
  return isNodeEnabled(node);

const node = {};

However, as @sledetman mentioned in the comments below your question, the code snippet you provided does not “return a boolean if tree has at least one enabled value”.