-
-
Save rodionchernyshov/6de81338d3f9d50210c33decf245ab28 to your computer and use it in GitHub Desktop.
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
// function isTokenExist() { | |
// return new Promise((resolve, reject) => { | |
// if (Math.random() >= 0.5) { | |
// return resolve({"token": 4321}) | |
// } | |
// reject("not exist") | |
// }) | |
// } | |
// | |
// | |
// | |
// getToken().then(token => { | |
// console.log(token) | |
// }); | |
function getToken() { | |
} | |
function reg(url, params) { | |
return new Promise((resolve, reject) => { | |
let promise = fetch(url); | |
return promise.then(r => { | |
return r.json() | |
}) | |
.then(data => resolve(data)) | |
.catch(err => { | |
if (403 === parseInt(err.status, 10)) { | |
return getToken() | |
.then(token => reg(url, params).then(data => resolve(data))) | |
.catch(err => reject(err)) | |
} else if (423 === parseInt(err.status, 10)) { | |
return generateToken() | |
.then(token => reg(url, params).then(data => resolve(data))) | |
.catch(err => reject(err)) | |
} | |
}); | |
}) | |
} | |
reg('https://randomuser.me/api/').then(data => console.log(data)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment