[英]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
require
和module.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;
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.