Skip to content

Instantly share code, notes, and snippets.

@kuznetsovandrey76
Created January 12, 2017 17:38
Show Gist options
  • Save kuznetsovandrey76/b15981c35c9a51b1cb1df7d41dafa34c to your computer and use it in GitHub Desktop.
Save kuznetsovandrey76/b15981c35c9a51b1cb1df7d41dafa34c to your computer and use it in GitHub Desktop.
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://kuznetsovandrey76.github.io/chingu-map/data.json');
xhr.send();
xhr.onload = function() {
console.log('answer:' + xhr.response);
};
@kuznetsovandrey76
Copy link
Author

var link = 'https://kuznetsovandrey76.github.io/chingu-map/data.json';

function loadUrl(url) {
	return new Promise(function(resolve, reject) {
 		var xhr = new XMLHttpRequest();
    xhr.open('GET', url);
    xhr.responseType = 'json'; // for JSON
    xhr.send();
    xhr.onload = function() {
      resolve(xhr.response);
    };
    xhr.onerror = function() {
     reject(xhr.statusText);
    };
  });
}
loadUrl(link).then(function(value) {
	console.log('first file onload');
	console.log(value);
  
  return loadUrl(link);
}).then(function(value) {
		console.log('second file onload');
		console.log(value);
}
// , function(error) {
//	   console.log(error); // You need choose. Second function equal catch
// }
).catch(function(error) {
  console.log(error);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment