<script>
function xhr(url) {
  return new Promise(function(resolve, reject) {
      var request = new XMLHttpRequest();
      request.addEventListener(
        'error',
        function(event) { reject(event); });
      request.addEventListener(
        'load',
        function(event) { resolve(request.response); });
      request.open('GET', url);
      request.send();
    });
}

function load_image(url) {
  return new Promise(function(resolve, reject) {
      var img = document.createElement('img');
      document.body.appendChild(img);
      img.onload = resolve;
      img.onerror = reject;
      img.src = url;
    });
}

function load_audio(url) {
  return new Promise(function(resolve, reject) {
      var audio = document.createElement('audio');
      document.body.appendChild(audio);
      audio.oncanplay = resolve;
      audio.onerror = reject;
      audio.src = url;
    });
}
</script>
