0

I have a three different Node.js files doing a bunch of async operations in each of them . I have to execute these files one after the other . Is there a batch process though which I can achieve this ? .

Ps: I don't want to use promises or module export

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
karthik006
  • 886
  • 9
  • 19
  • 1
    `node file1.js && node file2.js...`? – Phix Oct 18 '17 at 23:19
  • @phix . I tried that but it stops after executing the first file. Any idea why? – karthik006 Oct 18 '17 at 23:20
  • You can manually code anything yourself, but not building upon the tools that the language contains (like promises) is kind of silly. They are exactly what you should use for sequencing async operations for a whole host of reasons. Here's a look at various ways to do this: [How to synchronize a sequence of promises?](https://stackoverflow.com/questions/29880715/how-to-synchronize-a-sequence-of-promises/29906506#29906506). – jfriend00 Oct 19 '17 at 05:24

1 Answers1

0

Does the first command exit with a nonzero status code? Try running

node file1.js
echo $?

The bash && operator actually works rather like the Javascript && operator. In bash, if the first command exits with a nonzero status code, the operator returns that status code and stops. If the first command exits with a 0 status code, the operator executes the second command and returns its status code.

To run both commands regardless of status code, use ; instead of &&.

jcarpenter2
  • 5,312
  • 4
  • 22
  • 49