const fs = require('fs'); fs.readFile('./package.json',(err,data) => { if(err) return console.log(err); data =JSON.parse(data) console.log(data) })
const fs = require("fs"); function promiseFun(path) { console.log(path) return new Promise((resolve, reject) => { fs.readFile(path, (err, data) => { if (err){ reject(err)} else{ resolve(data);} }); }); } promiseFun('./package.json') .then(data => { data = JSON.parse(data); console.log(data.name); }) .catch(err => { console.log("err"); });
const fs = require("fs"); const util = require('util'); util.promisify(fs.readFile)('./package.json') .then(JSON.parse) .then(data => { console.log(data.name); }) .catch(err => { console.log(err) })
const fs = require("fs"); const util = require('util'); const readAsync=util.promisify(fs.readFile); async function init() { try { let data = await readAsync('./package.json') data = JSON.parse(data); console.log(data); }catch(err){ console.log(err) } }