简体   繁体   中英

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. 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.

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

One solution that immediately jumps out is to use require instead of using 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 allows you to run a promise synchronously by passing that function to it.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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