function readFile(filePath, opts, callback, count){ // callback(err)
    if(arguments.length===2){
        callback = arguments[1];
        opts = null;
    }
    if(typeof callback !== 'function') throw new Error('Wrong arguments');
    
    filePath = path.resolve(filePath);
    count = count || 0;
    var maxLimit = 5; // 5 * 500ms = 2,5s
    
    fs.readFile(filePath, opts, function(err, data) {
        if(err) {
            if(count >= maxLimit) callback(new Error('fsExt.readFie: max repeat limit reached').cause(err)); // repeat limit reached
            else setTimeout(function(){
                readFile(filePath, opts, callback, count + 1);
            }, 500);
        }
        else callback(null, data);
    });
}
function readFile(filePath, opts, callback, count){ 
    if(arguments.length===2){
        callback = arguments[1];
        opts = null;
    }
    if(typeof callback !== 'function') throw new Error('Wrong arguments');
    
    filePath = path.resolve(filePath);
    count = count || 0;
    var maxLimit = 5; 
    
    fs.readFile(filePath, opts, function(err, data) {
        if(err) {
            if(count >= maxLimit) callback(new Error('fsExt.readFie: max repeat limit reached').cause(err)); 
            else setTimeout(function(){
                readFile(filePath, opts, callback, count + 1);
            }, 500);
        }
        else callback(null, data);
    });
}
function readFile ( filePath , opts , callback , count ) { // callback(err) if ( arguments . length === 2 ) { callback = arguments [ 1 ] ; opts = null ; } if ( typeof callback !== 'function' ) throw new Error ( 'Wrong arguments' ) ; filePath = path . resolve ( filePath ) ; count = count || 0 ; var maxLimit = 5 ; // 5 * 500ms = 2,5s fs . readFile ( filePath , opts , function ( err , data ) { if ( err ) { if ( count >= maxLimit ) callback ( new Error ( 'fsExt.readFie: max repeat limit reached' ) . cause ( err ) ) ; // repeat limit reached else setTimeout ( function ( ) { readFile ( filePath , opts , callback , count + 1 ) ; } , 500 ) ; } else callback ( null , data ) ; } ) ; }
very simple read repeater, when gets error, try repeat after 500ms, but max 5 times
@param {String} filePath
@param {Function} callback(err, data)
very simple read repeater when gets error try repeat after 500ms but max 5 times