Skip to content

Instantly share code, notes, and snippets.

@kasecato
Created October 14, 2015 05:33
Show Gist options
  • Save kasecato/7a83d10fb51ac45f1202 to your computer and use it in GitHub Desktop.
Save kasecato/7a83d10fb51ac45f1202 to your computer and use it in GitHub Desktop.
// ES7 code, with async/await
async function xhrEdgeAsync(/* String */ url) {
let request = new XMLHttpRequest();
await request.open('GET', url, false);
await request.send();
if (request.status == 200) {
return request.responseText;
} else {
throw Error(request.statusText);
}
}
window.addEventListener("load", function() {
const SVG_FILE = 'data/lena.svg';
const JSON_FILE = 'data/bigdata.json';
const svg = document.getElementById('svg');
const json = document.getElementById('json');
const log = document.getElementById('log');
const logger = (/* String */ mess) => {
};
/*-----------------------------------------------------------------------*\
* Async
\*-----------------------------------------------------------------------*/
const edgeAsync = document.getElementById('edgeAsync');
edgeAsync.onclick = async () => {
var startTime = new Date();
for(var i = 0; i < 1000; i ++) {
// clear
svg.innerHTML = '';
json.innerText = '';
/* SVG */
logger('[START] Async SVG');
try {
const responseSvg = await xhrEdgeAsync(SVG_FILE);
logger('[END] Async SVG');
//svg.innerHTML = responseSvg;
} catch (e) {
logger('[END] Async SVG Error ' + e);
}
/* JSON */
logger('[START] Async JSON');
try {
const responseJson = await xhrEdgeAsync(JSON_FILE);
logger('[END] Async JSON');
//json.innerText = responseJson;
} catch (e) {
logger('[END] Async SVG Error ' + e);
}
}
var endTime = new Date();
let date = new Date();
log.value += '[EDGE]\t' + date.toLocaleDateString() + ' ' + date.toLocaleTimeString() + ': ' + (endTime - startTime) + "ms" + '\r\n';
};
/*-----------------------------------------------------------------------*\
* Sync
\*-----------------------------------------------------------------------*/
const edgeSync = document.getElementById('edgeSync');
edgeSync.onclick = () => {
// clear
svg.innerHTML = '';
json.innerText = '';
/* SVG */
logger('[START] Sync SVG');
xhrEdgeAsync(SVG_FILE).then(function (responseSvg) {
logger('[END] Sync SVG');
svg.innerHTML = responseSvg;
}).catch(function (error) {
logger(error);
});
/* JSON */
logger('[START] Sync JSON');
xhrEdgeAsync(JSON_FILE).then(function (responseJson) {
logger('[END] Sync JSON');
json.innerText = responseJson;
}).catch(function (error) {
logger(error);
});
};
}, false);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment