Created
August 29, 2019 07:08
-
-
Save Manyaka/618c09b085ad30c4879ebf6e0529fff8 to your computer and use it in GitHub Desktop.
ajax on promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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