MCPcopy Create free account
hub / github.com/azat-co/practicalnode / readJSON

Function readJSON

code/ch14/promise/readJSON.js:2–15  ·  view source on GitHub ↗
(filename, enc='utf8')

Source from the content-addressed store, hash-verified

1const fs = require('fs')
2function readJSON(filename, enc='utf8'){
3 return new Promise(function (resolve, reject){
4 fs.readFile(filename, enc, function (err, res){
5 if (err) reject(err);
6 else {
7 try {
8 resolve(JSON.parse(res))
9 } catch (ex) {
10 reject(ex)
11 }
12 }
13 })
14 })
15}
16
17readJSON('./package.json').then(console.log)

Callers 1

readJSON.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected