Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. let jsonParser = (function() {
  2.     let that = {
  3.         jsonResponse: null,
  4.         parsedJSON: null,
  5.         load: function(filepath, callback) {
  6.             let jsonObject = new XMLHttpRequest();
  7.             jsonObject.overrideMimeType("application/json");
  8.             jsonObject.open('GET', filepath, true);
  9.             jsonObject.onreadystatechange = function() {
  10.                 if (jsonObject.readyState == 4 && jsonObject.status == "200") {
  11.                     callback(jsonObject.responseText);
  12.                 }
  13.             }
  14.             jsonObject.send(null);
  15.         },
  16.         parse: function() {
  17.             let parsedJSON = JSON.parse(that.jsonResponse);
  18.             return that.parsedJSON;
  19.         },
  20.     };
  21.     return {
  22.         load: that.load,
  23.         parse: that.parse,
  24.     };
  25. })();
  26.  
  27. let response;
  28. jsonParser.load('data.json', () => {response = jsonParser.parse});
  29. console.log(response);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement