Skip to content

Instantly share code, notes, and snippets.

@Manyaka
Created August 29, 2019 07:08
Show Gist options
  • Save Manyaka/618c09b085ad30c4879ebf6e0529fff8 to your computer and use it in GitHub Desktop.
Save Manyaka/618c09b085ad30c4879ebf6e0529fff8 to your computer and use it in GitHub Desktop.
ajax on promise
const getAjax = url => {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.responseType = 'json';
xhr.addEventListener('load', () => {
if (xhr.status === 200) {
resolve(xhr.response);
} else {
reject('Ошибка на сервере, повторите попытку позже!');
}
});
xhr.addEventListener('error', () => {
reject('Ошибка на сервере, повторите попытку позже!');
});
xhr.send();
});
};
getAjax(myURL)
.then(data => data)
.catch(err => err);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment