Thursday, 15 September 2016

Wait for File in Node.js

/**
 * Created by Anand Dwivedi.
 */

/* ------------required import statement--------------  */
var fs = require("fs");
var userhome = require('user-home');

function readResponseFile() {
    var filepath =userhome+"\\"+"ECommerceDeliveryResponse.json";
    console.log(filepath);
    fs.stat(filepath, function (err, stats) {
     if(err){
         console.log("wait for File");
         setTimeout(readResponseFile, 3000);
     }else{
         console.log("file found")
     }
});
}

/* call the readResponseFile  of code right away */
readResponseFile();

2 comments:

  1. @ Anand is't it hardcoded for 3000 ms . what will happen if file is not found within that time span.

    ReplyDelete
  2. @Antrish Only time limit is hard coded . after 3 NS it will call same function and check file is there or not just like we are using Thread.sleep(30); . practically here also 30 is hardcoded . it is only for time limit .

    ReplyDelete