2

I have a question about node.js/javascript. It might be a little bit of a beginner question so feel free to see what solution I should use. I have a .js file that I execute with the below command in the commandpromt.

node test.js

When executing that command, it takes about 3 seconds for the commandprompt to log "start". Is this because it takes 3 seconds to load the overhead in the beginning?

For example if looking at the below code. I would like the javascript to wait for a file "C:/test/test.txt" and when this file exists, start to execute the code below. But if I need to execute the whole test.js file it will first take 3 seconds to load the overhead?

Please ask if I am not clear enought but, - I beleive my question is how it is possible to either already have the overhead loaded/cached for the code to wait for the file and instantly run the code?

'use strict';
const ccxt = require('ccxt');
const fs = require('fs');
const path = require('path');



//Here, I would like to let the code below react INSTANTLY 
//when for example this file Exists: "C:/test/test.txt"

//It takes 3 seconds before the code starts here
//Here the code begins.
console.log(`start`);
Andreas
  • 1,121
  • 4
  • 17
  • 34
  • What is your end goal? – Isaac Vidrine Mar 04 '19 at 16:37
  • The goal is to do a HTTP request when the file exist using the library "ccxt" – Andreas Mar 04 '19 at 16:38
  • it takes 150ms on my (rather old) system - are you sure there is nothing else going on ? – birdspider Mar 04 '19 at 16:41
  • @Andreas who creates the file? Will there be an arbitrary amount of files to watch? If its a user, why not just store that data in the db, then when a new record gets added make your http request. – Isaac Vidrine Mar 04 '19 at 16:42
  • Yes I am sure, I have done this 200 times, sometimes it takes 1 second, sometimes 2 and 3 seconds and I have a server also so the computer is good. I think I dont want to load the overhead somehow? The author of the library has suggested to not load the overhead each time but that is all I know and are told – Andreas Mar 04 '19 at 16:43
  • I do create the file from a C# application myself on the same computer. It will be between 3-10 files. I am not sure if I follow how to do that. How will that work? – Andreas Mar 04 '19 at 16:46
  • 1
    Okay, well If you're just trying to watch for new files or watch a file for changes, see this post https://stackoverflow.com/questions/13695046/watch-a-folder-for-changes-using-node-js-and-print-file-paths-when-they-are-cha – Isaac Vidrine Mar 04 '19 at 16:58
  • Thank you for your help! – Andreas Mar 04 '19 at 17:21

0 Answers0