简体   繁体   English

如何在每个节点完成任务后,在节点js中逐个地同步处理.JS文件?

[英]How to process .JS files individually in a synchronously fashion one by one in node js after each one of them have finished their tasks?

I have a set of .js files which perform certain tasks. 我有一组执行某些任务的.js文件。 Id like to process this files individually in a synchronously fashion after each one of them had finished their tasks. 我喜欢在每个文件完成任务后以同步方式单独处理这些文件。

Right now when I run my code all functions perform perfectly, yet in an async way. 现在,当我运行我的代码时,所有函数都以完全异常的方式执行。 Im afraid I have also tried with promises (take a look at //FILE parent v2 in code) yet it seems that nonetheless the task being executed in order yet not waiting to be processed one after the other. 我担心我也尝试过promises(看看代码中的FILE parent v2)但是似乎仍然按顺序执行的任务还没有等待一个接一个地处理。

I´m pretty sure there must be a basic solution to that issue. 我很确定必须有一个基本的解决方案来解决这个问题。 yet my programming skills are scarce. 但我的编程技巧很少。

thanks for understanding. 感谢您的理解。

right now my code looks like this: 现在我的代码看起来像这样:

 //FILE parent.js const cp = require('child_process'); const path = require('path'); //PROCESS1 var child_call = cp.fork("process1.js") child_call.on("exit", () => { console.log("1st funtion finished"); }) //PROCESS2 var child_capture = cp.fork("process2.js") child_capture.on("exit", () => { console.log("2nd funtion finished"); }) //PROCESS3 var child_render = cp.fork("process3.js") child_render.on("exit", () => { console.log("3rd funtion finished"); }) //FILE v2 Promisess parent.js const async = require('async'); const cp = require('child_process'); const path = require('path'); function addPromise() { return new Promise(resolve => { var child_call = cp.fork("process1.js") child_call.on("exit", () => { console.log("1st funtion finished"); }) resolve() }); } function addCapture() { return new Promise(resolve => { var child_capture = cp.fork("process2.js") child_capture.on("exit", () => { console.log("2nd funtion finished"); }) resolve() }); } function addRender() { return new Promise(resolve => { var child_render = cp.fork("process3.js") child_render.on("exit", () => { console.log("3rd funtion finished"); }) resolve() }); } async function addAsync() { const a = await addPromise(); const b = await addCapture(); const c = await addRender(); return a + b + c; } addAsync().then((sum) => { console.log(sum); }); 

require and module.exports requiremodule.exports

One solution that immediately jumps out is to use require instead of using child_process.fork . 立即跳出的一个解决方案是使用require而不是使用child_process.fork This way your imported code will run synchronously and you will get a direct output returned. 这样导入的代码将同步运行,您将获得返回的直接输出。

Example: 例:

function add() {
  const a = require('a.js');
  const b = require('b.js');
  const c = require('c.js');
  return a + b + c;
}

// or you can make it more usable
function addModules(...fileNames) {
  return fileNames
    .map(fileName => require(fileName))
    .reduce((total, x) => total + x, 0);
}

Do note that you will be required to export your results from those files if you wish to use them. 请注意,如果您希望使用这些文件,需要从这些文件中导出结果

// Do your stuff here:
const x = 42;

// Export it
module.exports = x;

Or you can use deasync 或者您可以使用deasync

Deasync allows you to run a promise synchronously by passing that function to it. Deasync允许您通过将该函数传递给它来同步运行promise。

Example: 例:

const cp = require('child_process');
const deasync = require('deasync');

const fork = deasync(cp.fork);

function addPromise() {
  const child_call = fork('process1.js');
  // now synchronous
};
// repeat as needed

function add() {
  const a = addPromise();
  // ...
  return a + b + c;
}

Note: deasync exposes implementation details of Node.js at the language level and as such shouldn't be used unless other safer solutions don't work for you. 注意: deasync在语言级别公开Node.js的实现细节,因此除非其他更安全的解决方案不适合您,否则不应使用。

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

相关问题 如何在Node.js中一一下载文件? - How to download files one by one in node js? 在 Vue JS 中完成加载前一个项目之后加载每个项目 - Load each item after the one before is finished loading in Vue JS 第一个结束后如何在 Node.js 中生成新的 Python 子进程? - How to spawn a new Python child process in Node.js after the first one ends? 在构建过程中将多个.js文件合并为一个 - combining multiple .js files into one in a build process 如何在Node js中下载捆绑成一个zip文件的多个文件? - How to download multiple files bundled into one zip files in Node js? 将text.js与require.js结合使用时,如何创建一个模板html文件,但分别获取每个脚本模板? - Using text.js with require.js how can I create one template html file but grab each script template individually? 如何使用node命令一个接一个地运行多个.js文件? - How to use node command to run multiple .js files one after the other? 如何在Node JS中的不同文件之间共享一个数据库连接? - How to share one database connection across different files in node js? 如何在多个文件 node.js 中使用一个 function - how to use one function within multiple files node.js Node.js异步任务是否同步处理? - Are Node.js asynchronous tasks handled synchronously?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM