How to deal with the asynchronous result of node.js export

  1. the project obtains the configuration items through the configuration file before it wants to go to devops,. Now it pulls the configuration items from the configuration center, modifies the previous configuration file Synchronize load to obtain the configuration items in etcd through the API, and finds that the asynchronously obtained results cannot be exported?

the code is as follows:

-sharp 
var baseConfigFilePath      = "../base-config.yaml";  // 
var customConfigFilePathEnv = "CONFIG_PATH";  // 

var loadFile = exports.loadFile = function loadFile() {
  var baseConfigFileFullPath = path.join(__dirname, baseConfigFilePath);
  var customConfigFilePath   = process.env[customConfigFilePathEnv];


  var fileData, obj;



  /*  */
  fileData = fs.readFileSync(baseConfigFileFullPath);
  obj = yaml.load(fileData);

   // 
  if (!toolkit.isNullOrWhiteSpace(customConfigFilePath)) {
    var customFileData = fs.readFileSync(customConfigFilePath);
    var customObj = yaml.load(customFileData);
    
    // 
    toolkit.objUpdate(customObj, obj);
  }

  exports.CONFIG = obj;
};

loadFile();

modified

var loadFile = exports.loadFile = function loadFile() {
  var baseConfigFileFullPath = path.join(__dirname, baseConfigFilePath);
  var customConfigFilePath   = process.env[customConfigFilePathEnv];

  var fileData, obj;


  /*  */
  fileData = fs.readFileSync(baseConfigFileFullPath);
  obj = yaml.load(fileData);

  // 
  if (!toolkit.isNullOrWhiteSpace(customConfigFilePath)) {
    // var customFileData = fs.readFileSync(customConfigFilePath);
    // var customObj = yaml.load(customFileData);
    request(customConfigFilePath, function(error, response, data) {
      if (error) {
        console.log("", colors.red(""));
      }
      var resData = JSON.parse(data);

      if (resData.errorCode) {
        console.log("", colors.red(""));
      }

      var customObj = toolkit.fromBase64(resData.node.value);
      toolkit.objUpdate(customObj, obj);
    })
  }

  exports.CONFIG = obj;
};

exports is handled by Synchronize. The data obtained by etcd cannot be exported. How to deal with this problem while keeping the logic of the source code unchanged?


1, turn request into
2 of Synchronize. Here's what I think:
module:test1.js

 

exports a Promise , while the outside is promise.then , whether it's async/await , it's an external matter.

secondly, I think the intrusion into the configuration center here is too strong, which will affect too many processes. I think the configuration center is equipped with agent just do the configuration file Synchronize (or other functional extensions are also put here, and have nothing to do with the service as far as possible), and read it locally, so that all services cannot be started after the configuration center is hung up.

Menu