title: Re run function if error node.js
body: <p>I have a function that reads in multiple JSON files in a loop. Problem is, they are being updated every fraction of a second via a websocket. so every so often this error (im guessing because its trying to read the file whilst its being updated) appears: </p>
  
  <pre><code>QSPBTC  rsi 48.09
  BTSBTC  rsi 40.36
  undefined:1
  
  SyntaxError: Unexpected end of JSON input
  at JSON.parse (&lt;anonymous&gt;)
  at checkPairs (C:\Users\user\Documents\bot\deletesoon.js:39:24)
  at runCheckPairs (C:\Users\user\Documents\bot\deletesoon.js:81:5)
  at Object.&lt;anonymous&gt; (C:\Users\user\Documents\bot\deletesoon.js:88:1)
  at Module._compile (module.js:652:30)
  at Object.Module._extensions..js (module.js:663:10)
  at Module.load (module.js:565:32)
  at tryModuleLoad (module.js:505:12)
  at Function.Module._load (module.js:497:3)
  at Function.Module.runMain (module.js:693:10)
  at startup (bootstrap_node.js:188:16)
  at bootstrap_node.js:609:3
  </code></pre>
  
  <p>So I trying To make a function that catches this error, and allows the function to start again (I also have the function being called every 5 minutes.)</p>
  
  <pre><code>function checkPairs(){
  
  var RSI = require('technicalindicators').RSI;
  const fs = require('fs');
  var Twit = require('twit');
  var config = require('./tconfig');
  var T = new Twit(config);
  var message = [];
  var theLoop = ['ETHBTC','LTCBTC','BNBBTC','NEOBTC','BCCBTC','GASBTC'];
  
  
  
  
  for(i = 0; i &lt; theLoop.length; i++){
  
  var symbol = theLoop[i];
  var data = fs.readFileSync('charts/'+symbol+'.json');
  var CurrentPair = JSON.parse(data);
  
  var prices = [];
  var period = 14;
  
  var lowTotal = [];
  var closeTotal = [];
  var rsiArray = [];
  
  for (var item in CurrentPair) {
  prices.push(Number(CurrentPair[item].close));
  } 
  
  
  var newRSI = [];
  newRSI = RSI.calculate({period : period, values : prices});
  var rsiResult = newRSI[newRSI.length -1];
  console.log(symbol + "  rsi " + rsiResult);
  
  if(rsiResult &lt;= 23 ){
  var twitMessage = "RSI is LOW ("+rsiResult+") ";
  var splitSymbol = "";
  
  if(symbol.includes("BTC", 3) || symbol.includes("BTC", 4) || 
  symbol.includes("BTC", 5) || symbol.includes("BTC", 6) ){
  splitSymbol = symbol.replace("BTC", "");
  };
  
  T.post('statuses/update', { status: 'INDICATOR ALERT: '+ symbol + ' 
  #'+splitSymbol +' : '+ twitMessage + ' \n \n #'+splitSymbol + ' #binance' + 
  '   #dayTrading' + ' #TradingView'}, function(err, data, response) {
  console.log('                              TWEETED  ' + symbol + "  RSI " + 
  rsiResult);
  })}; 
  }; 
  };
  
  
  var runCheckPairs = function () {
  setInterval(checkPairs, 300000);
  checkPairs();
      if(err){
          console.log("error Handled")
          runCheckPairs();
      }
  };
  
  runCheckPairs();
  console.log("\n\n");
  </code></pre>
  
  <p>Any help would be great thanks</p>