Restart a promise after fail

前端 未结 1 1727
执念已碎
执念已碎 2021-01-24 07:03

I\'m using Nodejs and Q to run a sequence of asynchronous functions. If one fails i\'d like to run another function and then start the sequence again. Heres it is as is:

相关标签:
1条回答
  • 2021-01-24 07:39

    You need to wrap it in a function that you can call again. A promise by itself cannot be "restarted".

    var promise = (function trySearch() {
        return database.getUserCookies(user)
        .then(proxy.search)
        .fail(function(err) {
            if (err.rejected === 302) { // only when missing authentication
                return database.fetchAuth(data)
    //          ^^^^^^
                .then(proxy.login)
                .then(database.saveCookies)
                .then(trySearch) // try again
            } else
                throw err;
        })
    }())
    .then(responser.search)
    
    0 讨论(0)
提交回复
热议问题