Skip to content

Instantly share code, notes, and snippets.

@stepanmas
Last active December 27, 2018 12:22
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 stepanmas/d755c5f74d8c6b2bc04273f3b908c9f5 to your computer and use it in GitHub Desktop.
Save stepanmas/d755c5f74d8c6b2bc04273f3b908c9f5 to your computer and use it in GitHub Desktop.
Собеседование: промис 2
const url = 'http://lol';
function apiGet(url, attempts = 5) {
return new Promise(function core(resolve, reject) {
fetch(url)
.then((res) => {
resolve(res)
})
.catch(() => {
if (attempts) {
attempts--;
core(resolve, reject);
} else {
reject('Error text')
}
})
});
}
apiGet(url)
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err)
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment