Skip to content

Commit

Permalink
Save promises of API calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
luckytyphlosion committed Aug 2, 2020
1 parent ea337d9 commit a675d98
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
1 change: 0 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,5 @@
</head>
<body>
<table id="mcceQueue"></table>
<pre></pre>
</body>
</html>
15 changes: 14 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ var runData = null;
var categoryData = null;
var variableData = null;

var apiCallSavedPromises = new Map();

// Taken and modified from https://stackoverflow.com/a/29153059
var srcIso8601DurationRegex = /PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)(?:\.(\d+))?S)?/;

Expand All @@ -30,8 +32,16 @@ function parseSrcISO8601Duration(srcIso8601Duration)

function srcApiGetFromUrlAwait(urlStr)
{
console.log(`Starting ${urlStr} at ${performance.now()}.`);

let savedPromise = apiCallSavedPromises.get(urlStr);
if (savedPromise !== undefined) {
console.log(`${urlStr}: using saved result`);
return savedPromise;
}

let timeStart = performance.now();
return new Promise(function (resolve, reject) {
let promise = new Promise(function (resolve, reject) {
let xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", urlStr, true);
xmlHttp.onload = function () {
Expand All @@ -55,6 +65,9 @@ function srcApiGetFromUrlAwait(urlStr)
};
xmlHttp.send();
});

apiCallSavedPromises.set(urlStr, promise);
return promise;
}

function srcApiGetAwait(endpoint)
Expand Down

0 comments on commit a675d98

Please sign in to comment.