有没有办法在Javascript中同步等待递归异步函数的完成?

时间:2017-06-05 03:50:37

标签: javascript node.js asynchronous

我在node.js中编写了以下javascript代码:

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.");

它是我正在处理的应用程序的测试版本。我想保留copy()函数的异步性质,但是我也想知道完整副本何时完成(因为与entities相关的代码暗示)。理由是因为我希望能够告诉它何时完成,还要实现进度跟踪功能。

有没有办法在Javascript中同步等待递归异步函数的完成?

1 个答案:

答案 0 :(得分:-1)

这可以通过顺序执行器nsynjs来完成。您的代码将转换如下:

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");

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