How is nodeJS’s logic to reacquire token designed?

  node.js, question

If I need to get user information through token, there is such a function:

function getUserInfo(token, onSuccess, onFail) {}

Then if the token fails, I need to reacquire the token first and then reacquire the user function, which is probably as follows:

getUserInfo(token, onSuccess, onFail(err) {
 // if token fails
 getNewToken(onSuccess(tkn) {
 getUserInfo(tkn, onSuccess, onF);
 });
 });

However, if you write like this, if you fail to reacquire a token, or you need to limit the number of times you reacquire a token, the code will be messy and difficult to organize. Is there a better way to write it? I know a little about promise, but I don’t know how to apply it to such a scene.

The implementation of promise mode, I am currently doing this:

//Record the number of repeated attempts to obtain Token
 var count = 0;
 
 //Maximum number of repetitions limit
 var limit = 3;
 
 //get Token
 function getToken() {
 return new Promise(...);
 bracket
 
 //Get Users
 function getUser() {
 return new Promise(function(resolve, reject) {
 if (token is invalid) {
 getToken().then(function() {
 resolve(getUser());
 }, function(rej) {
 //Repeated attempts to obtain Token
 If (plus count < limit) {
 resolve(getUser());
 bracket
 else {
 reject(rej);
 bracket
 });
 bracket
 
 count = 0;
 resolve(user)
 });
 bracket

Note: If you are in a concurrent environment, count cannot exist as a External variable, but can only be placed in context to avoid erroneous counting.