0

I am aware of callback functions in javascript but I am not very much expert in this synchronous-asynchronous callbacks. Hence I am facing some issues as described below: I have following code:

function (core, node, callback) 
{
    var result = {hasViolation: false, message: ''};
    // Here goes the constraint checking..
    var parentNode = core.getAttribute (core.getParent(node), 'name' );
    var nodeName = core.getAttribute(node,'name');
    var path = core.getPath(node);
    var dstPathCollection = core.getCollectionPaths(node, 'dst');
    core.loadChildren(node,function (err,children) 
    { 
      if(err)
      {
        result.message += "error during loading of node\'s children\n";
        error = error || err;
      }
      if (children.length > 0)
      {
        var child_i = 0;
        for (child_i=0; child_i< children.length; child_i++ )
        {
            var child_node = children[child_i];
            var name = core.getAttribute(child_node, 'name');
            var child_path = core.getPath(child_node);
            var meta = core.getBaseType(child_node);
            var metaName = meta ? self.core.getAttribute(meta, 'name') : ':LibraryRoot:';

            if(metaName == 'InputPort')
            {
              core.loadCollection(child_node, 'dst', function (err, connNodes) 
              {
                if (err) 
                {
                  // Handle error
                  console.log("Error, Oops");
                }
                if(connNodes.length == 0)
                {
                  //LINE ABCDE
                  result.hasViolation = true;
                  result.message = "For Gate "+nodeName+" with path "+ path + 
                              " input port with path " + child_path + " is unconnected.";
                  return result;
                }
              });
              console.log(result.hasViolation + " %% " + result.message );
            }
        } 
      }
      else
      {
        result.hasViolation = true;
        result.message = nodeName + ' with path ' + path + ' has no port defined';
      }
    });


    //LINE WXYZ
    console.log(result.hasViolation + " ~~ " + result.message );
    callback(null, result)
}

How will I change above code so that changes made to variable result after line after comment //LINE ABCD will be reflected in the lines after comment //LINE WXYZ?

Edit: I found a way to solve this problem and I am going to write the answer so that anyone who faces similar problem may get benefit from it.

Following is the version, that worked for me:

function (core, node, callback) 
{
    var result = {hasViolation: false, message: ''};
    var error = null;
    // Here goes the constraint checking..
    var parentNode = core.getAttribute (core.getParent(node), 'name' );
    var nodeName = core.getAttribute(node,'name');
    var path = core.getPath(node);
    var dstPathCollection = core.getCollectionPaths(node, 'dst');
    var checkPortOfGate = function()
    {
        core.loadChildren(node,function (err,children) 
        { 
          if(err)
          {
            result.message += "error during loading of node\'s children\n";
            error = error || err;
            return checkingDone();
          }

          if (children.length > 0)
          {
            var child_i = 0;
            for (child_i=0; child_i< children.length; child_i++ )
            {
                var child_node = children[child_i];
                var name = core.getAttribute(child_node, 'name');
                var child_path = core.getPath(child_node);
                var meta = core.getBaseType(child_node);
                var metaName = meta ? self.core.getAttribute(meta, 'name') : ':LibraryRoot:';

                if(metaName == 'InputPort')
                {

                  core.loadCollection(child_node, 'dst', function (err, connNodes) 
                  {
                    if (err) 
                    {
                      // Handle error
                      console.log("Error, Oops");
                      return checkingDone();
                    }
                    if(connNodes.length == 0)
                    {
                      //LINE ABCDE
                      result.hasViolation = true;
                      result.message = "For gate "+nodeName+" with path "+ path + 
                                  ", input port with path " + child_path + " is unconnected.";
                      return checkingDone();
                    }
                  });
                  console.log(result.hasViolation + " %% " + result.message );
                }
                else
                {
                    return checkingDone();
                }
            } 
          }
          else
          {
            result.hasViolation = true;
            result.message = nodeName + ' with path ' + path + ' has no port defined';
            console.log(result.hasViolation + " ^^ " + result.message );
            return checkingDone();
          }
        });
    },

    checkingDone = function(){
            callback(error,result);
    };
    checkPortOfGate();
}

0 Answers0