How to enter error handling after three errors in promise handling asynchrony?

encountered an interview question, usually after an asynchronous operation error will enter. Catch (), now has to enter after three errors, how to achieve this.

Sep.20,2021

first of all, this topic is problematic. Once the Promise state is determined, it will not change, so there is no point in handling multiple errors with the same Promise .
the meaning of the comment is actually ajax request 3 times, so give me a chestnut

.
let Ajax = function(option) {
    console.log('ajax')
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            reject('some error ')
            // resolve()
        }, 1000)
    })
}

function runAjax(option, retry = 2) {
    let p = Ajax(option);
    if (retry > 0) {
        retry--;
        return new Promise((resolve, reject) => {
            p.then(resolve).catch(() => {
                resolve(runAjax(option, retry))
            })
        })
    }
    return p;
}


runAjax({ hello: 'word' }).then(() => {
    console.log('success')
}).catch((err) => {
    console.log('run error', err)
})

can I say I don't understand? Is that what the interview questions are like?
Menu