Skip to content

Instantly share code, notes, and snippets.

@Rich-Harris
Last active September 23, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Rich-Harris/0af262f1bdebaeebfe2c to your computer and use it in GitHub Desktop.
Save Rich-Harris/0af262f1bdebaeebfe2c to your computer and use it in GitHub Desktop.
XHR demo
function get ( url ) {
return new Promise( function ( fulfil, reject ) {
var xhr = new XMLHttpRequest();
xhr.open( 'GET', 'my-data.json' );
xhr.onload = function () {
fulfil( xhr.responseText );
};
xhr.onerror = reject;
xhr.send();
});
}
<main>hello <span class='target'>[click me to load my-data.json]</span>!</main>
<script src='get.js'></script>
<script>
var target = document.querySelector( '.target' );
target.addEventListener( 'click', function () {
get( 'my-data.json' )
.then( JSON.parse )
.then( function ( data ) {
target.textContent = data.hello;
});
});
</script>
{
"hello": "world"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment