【问题标题】:How to use fs.createReadStream() in sync without call back如何在不回调的情况下同步使用 fs.createReadStream()
【发布时间】:2020-10-28 07:35:05
【问题描述】:

我打算从 zip 中复制一个文件内容并将二进制内容放在另一个文件中。当我需要一个包时将使用它。

例子:-

      func_name(CURRENT_DIR);
      const MainController = require('./src/controllers/mainController');
      // This mainController file will require the binary file which is create the func_name function
      controller = new MainController(
        context,
        db2ConnectOutputChannel,
        undefined /*vscodeWrapper*/
      );
      context.subscriptions.push(controller);
      controller.activate();

func_name 定义

        var odbcBindingsNode;
        var ODBC_BINDINGS = path.resolve(CURRENT_DIR, 'node_modules\/ibm_db\/build\/Release\/odbc_bindings.node');
        odbcBindingsNode = 'build\/Release\/odbc_bindings_e' + electron_version + '.node';
        readStream = fs.createReadStream(BUILD_FILE);
        readStream.pipe(unzipper.Parse())
            .on('entry', function (entry) {
                if(entry.path === odbcBindingsNode) {
                    entry.pipe(fstream.Writer(ODBC_BINDINGS));
                } else {
                    entry.autodrain();
                }
            })
            .on('error', function(e) {
                console.log('Installation Failed! \n',e);
            })
            .on('finish', function() {
              console.log("\n" + 
              "===================================\n"+
              "installed successfully!\n"+
              "===================================\n");
              })

问题是第一个函数不会等到第二个函数完成。它移动到下一行并尝试要求需要此 .node 文件的主控制器文件并返回 .node 未找到。 但是 .node 是在调用 require 之后创建的。有没有办法让它同步?

我尝试了回调,它返回的要求不能在回调中使用。

回调代码:-

function akhil(CURRENT_DIR){
          var BUILD_FILE = path.resolve(CURRENT_DIR, 'folder\/build.zip');
          var odbcBindingsNode;
          var ODBC_BINDINGS = path.resolve(CURRENT_DIR, 'folder\/build\/Release\/odbc_bindings.node');
          odbcBindingsNode = 'build\/Release\/odbc_bindings_e' + electron_version + '.node'
          readStream = fs.createReadStream(BUILD_FILE);
          /*
            * unzipper will parse the build.zip file content and
            * then it will check for the odbcBindingsNode
            * (node Binary), when it gets that binary file,
            * fstream.Writer will write the same node binary
            * but the name will be odbc_bindings.node, and the other
            * binary files and build.zip will be discarded.
            */
            readStream.pipe(unzipper.Parse())
              .on('entry', function (entry) {
                  if(entry.path === odbcBindingsNode) {
                      entry.pipe(fstream.Writer(ODBC_BINDINGS));
                  } else {
                      entry.autodrain();
                  }
              })
              .on('error', function(e) {
                  console.log('Installation Failed! \n',e);
              })
              .on('finish', function() {
                console.log("\n" + 
                "===================================\n"+
                "installed successfully!\n"+
                "===================================\n");
                console.log("This is rebuild");
                const MainController = require('./src/controllers/mainController');
                controller = new MainController(
                  context,
                  db2ConnectOutputChannel,
                  undefined /*vscodeWrapper*/
                );
                context.subscriptions.push(controller);
                controller.activate();
                })
            return 1;
    }

【问题讨论】:

  • 能否贴出尝试使用回调的代码和错误信息
  • 没有错误消息..当我在回调中调用 require 时,它​​会停在那里。 cosole.log() 也不会在 require 之后打印。
  • 你知道,如果不看代码,真的很难诊断为什么“它停在那里”
  • @pspi 我已经添加了回调代码..请在问题部分查看
  • 这个解压缩功能看起来不错,虽然它很难阅读,但我相信一旦你开始工作,你就会重构它。接下来我要做的是将调试日志添加到所需的 mainController 文件中,问题可能就在那里。尝试查明它停止处理并开始挂起的位置。它挂起是因为根据您的描述,它不会返回并在其后打印以下 console.log 行。

标签: node.js fs vscode-extensions


【解决方案1】:

使用 ADM-zip 包我能够解决这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-18
    • 2020-06-18
    相关资源
    最近更新 更多