javascript - fs.statSync throws an error when contained in a function? -
i'm making function returns boolean when file exists or not, using fs.statsync. looks this:
function doesexist (cb) { let exists try { fs.statsync('./cmds/' + firstinitial + '.json') exists = true } catch (err) { exists = err && err.code === 'enoent' ? false : true } cb(exists) } example use case:
let fileexists doesexist('somefile.json', function (exists) { fileexists = exists }) however, running code throws me typeerror: string not function. have no idea why.
i think want remove callback, , add file name parameters:
function doesexist(firstinitial) { try { fs.statsync('./cmds/' + firstinitial + '.json') return true } catch(err) { return !(err && err.code === 'enoent'); } } let fileexists = doesexist('somefile'); btw, there fs.exists.
Comments
Post a Comment