You have a function authenticate
which returns a resolved promise on success or reject on failure. If the authentication failed the
process should be repeated. This can be a achieved with a recursive promise.
let counter = 0;
function login() {
return new Promise((resolve, reject) => {
if (counter === 0) { // will be true on the first call
counter++;
reject(new Error("Error logging in, please try again"));
} else {
resolve("Success");
}
})
.catch(err => {
console.log("Caught");
return login();
});
}
login().then((res) => {
console.log("Success", res);
}).catch(err => {
console.log("Error", err);
});
// The above will log
// Caught
// Success success