Guest User

Untitled

a guest
Oct 16th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.72 KB | None | 0 0
  1. fs = require("fs")
  2.  
  3. readJSON = (path, cb) ->
  4. fs.readFile path, "utf8", (err, data) ->
  5. if err
  6. return cb err
  7.  
  8. try
  9. json = JSON.parse(data)
  10. catch error
  11. return cb error
  12.  
  13. cb null, json
  14.  
  15.  
  16.  
  17. readJSONFiles = (files, cb) ->
  18. results = {}
  19. remaining = files.length
  20. files.forEach (file) ->
  21.  
  22. readJSON file, (err, json) ->
  23. if err
  24. return cb err
  25.  
  26. results[file] = json
  27.  
  28.  
  29. cb null, results unless --remaining
  30.  
  31. # cb(null,results)
  32.  
  33.  
  34. #files = ["wellformed.json","malformed.json","wellformed_2.json"]
  35. files = ["wellformed.json","wellformed_2.json"]
  36.  
  37.  
  38. readJSONFiles files, (err, data) ->
  39.  
  40. console.log "callback called"
  41. console.log data
  42.  
  43. console.log "end"
Add Comment
Please, Sign In to add comment