我将如何修改回调函数,以便一旦回调返回,全局变量的值就会改变?

时间:2016-11-27 08:31:01

标签: javascript node.js callback

我知道javascript中的回调函数,但我对这种同步异步回调并不是很专业。因此,我面临如下所述的一些问题: 我有以下代码:

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

我如何更改上面的代码,以便在评论// LINE ABCD之后的行后面对变量结果所做的更改将反映在注释// LINE WXYZ之后的行中?

编辑:我找到了解决这个问题的方法,我将写下答案,以便任何面临类似问题的人都可以从中受益。

以下版本对我有用:

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 个答案:

没有答案