简体   繁体   English

有没有办法同步等待Java中递归异步功能的完成?

[英]Is there a way to synchronously wait for the completion of a recursive asynchronous function in Javascript?

I have the following javascript code written to run in node.js: 我编写了以下javascript代码以在node.js中运行:

const fs = require("fs");
const path = require("path");

var copy = function(paramSource, paramDestination, paramCallback) {
  if (fs.existsSync(paramDestination)) {
    return paramCallback("Destination Exists"); //TODO make formal error code
  } else {
    fs.lstat(paramSource, function(error, sourceStats) {
      if (error) {
        return paramCallback(error);
      } else {
        if (sourceStats.isDirectory()) {
          fs.readdir(paramSource, function(error, sourceContents) {
            if (error) {
              return paramCallback(error);
            } else {
              fs.mkdir(paramDestination, sourceStats.mode, function(error) {
                if (error) {
                  paramCallback(error);
                } else {
                  if (sourceContents.length > 0) {
                    sourceContents.forEach(function(sourceChild) {
                      copy(path.join(paramSource, sourceChild), path.join(paramDestination, sourceChild), paramCallback);
                    });
                  }
                  return paramCallback(null, paramDestination);
                }
              });
            }
          });
        } else {
          var sourceFile = fs.createReadStream(paramSource);
          var destinationFile = fs.createWriteStream(paramDestination); //TODO permissions
          sourceFile.on("error", function(error) {
            return paramCallback(error);
          });
          destinationFile.on("error", function(error) {
            return paramCallback(error);
          });
          sourceFile.on("close", function() {
            //return paramCallback(null, paramSource);
          });
          destinationFile.on("close", function() {
            return paramCallback(null, paramDestination);
          });
          sourceFile.pipe(destinationFile);
        }
      }
    });
  }
};

var entities = 0;

copy("/home/fatalkeystroke/testing/directory_01", "/home/fatalkeystroke/testing/directory_01_copy", function(error, name) {
  if (error) {
    console.log(error);
  } else {
    console.log(name);
    entities++;
  }
});

console.log("Copied " + entities + " entities.");
console.log("Program Done.");

It's a testing exerpt from an application I'm working on. 这是我正在处理的应用程序的测试摘录。 I would like to keep the asynchronous nature of the copy() function, however I also want a way to know when the full copy is complete (as the code related to entities implies). 我想保留copy()函数的异步性质,但是我也想知道完整副本何时完成(正如与entities相关的代码所暗示的)。 The reasoning is because I want to be able to tell when it's done of course, but also implement a progress tracking capability. 这样做的原因是,我当然想知道何时完成,而且还实现了进度跟踪功能。

Is there a way to synchronously wait for the completion of a recursive asynchronous function in Javascript? 有没有办法同步等待Java中递归异步功能的完成?

This could be done with sequential executor nsynjs . 这可以通过顺序执行程序nsynjs完成 Your code will transform as follows: 您的代码将进行如下转换:

var copy = function (src,dst) {
    if(nsynFs.lstat(nsynjsCtx, src).data.isDirectory()) {
        var files = nsynFs.readdir(nsynjsCtx, src).data;
        nsynFs.mkdir(nsynjsCtx,dst);
        for(var i=0; i<files.length; i++) {
            var f=files[i];
            console.log(f);
            copy(src+"/"+f, dst+"/"+f);
        }
    }
    else { // is File
        var input = nsynFs.open(nsynjsCtx,src,'r').data;
        var output = nsynFs.open(nsynjsCtx,dst,'w').data;
        var buf = Buffer.alloc(4096);
        while(true) {
            var readCnt = nsynFs.read(nsynjsCtx,input,buf,0,4096,null).data;
            if(!readCnt)
                break;
            nsynFs.write(nsynjsCtx,output,buf,0,readCnt,null);
        }
        nsynFs.close(nsynjsCtx,input);
        nsynFs.close(nsynjsCtx,output);
    }
};

copy("test1","test2");

Please see full working example here: https://github.com/amaksr/nsynjs/blob/master/examples/node-copy-files/index.js 请在此处查看完整的工作示例: https : //github.com/amaksr/nsynjs/blob/master/examples/node-copy-files/index.js

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM