蓝鸟承诺重试DocumentDB请求 [英] Bluebird Promise Retry DocumentDB request

查看:88
本文介绍了蓝鸟承诺重试DocumentDB请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将带有回调的重试函数重写为Bluebird promise,但似乎无法正确地做到这一点.底部是工作的回调函数,用于在达到限制时重试Azure DocumentDB.我正在尝试在函数本身中使用promise,但是在到达"Then"之前它会返回.任何有关如何解决此问题的提示,或如果通过使用catch来影响性能的任何提示,都将不胜感激.谢谢!

I'm trying to rewrite a retry function with callbacks into a Bluebird promise one but can't seem to get my head around the correct way of doing this. At the bottom is the working callback function for retrying Azure DocumentDB when limit is met. I'm trying to use promises in the function itself but it returns before reaching the "Then". Any hints on how to tackle this or if performance is affected by using catch this way would be appreciated. Thank you!

"readDocRetry": function(id, retries) {
                var self = this;
                return new Promise(function(resolve, reject){
                    self.client.readDocumentAsync(self.docsLink + id, null, function(err, data){
                        if (err) {
                            reject(err);
                        } else {
                            resolve(data)
                        }
                    }).then(function(results) {
                        console.log("ReadDocRetry result: " + results)
                         return results;
                    }).catch(function(err, headers) {
                        RetryError(self, id, err, headers, retries);
                    });

                });
            }

function RetryError(self, id, err, headers, retries) {
    if (err && err.code) {
        if (err.code === 429 && retries >= 0) {
            setTimeout(function() {
                self.readDocRetry(id, retries - 1); 
            }, Number(headers['x-ms-retry-after-ms'] || 1));
        }
        else if (err.code === 503 && retries >= 0) {
            setTimeout(function() {
                self.readDocRetry(id, retries - 1) 
            }, 500); 
        }
    }
    else if(err) {
        console.log(err); 
    }else{
        console.log("Err missing in RetryError");
    }
}


    bbCtx.readDocRetry("19").then(function(res){
        console.log("Hurrah!" + res);
    })

-------使用传统回调的工作示例,我试图基于它进行承诺-----

------- Working example with traditional callbacks which I'm trying to make promise based -----

    dbContext.prototype = {
        readDocRetry: function (id, retries, cb) {
            var self = this;
            self.client.readDocument(self.docsLink + id, function (err, results, headers) {
                if (err) {
                    if (err.code === 429 && retries >= 0) {
                        var aR = retries - 1;
                        setTimeout(function () {
                                self.readDocRetry(id, aR, cb);
                        }, Number(headers['x-ms-retry-after-ms'] || 1));
                    } else if (err && err.code === 503 && retries >= 0) {
                        var aR = retries - 1;
                        setTimeout(function () {
                                self.readDocRetry(id, aR, cb)
                        }, 500); 
                    } else {
                        cb(err); 
                    }
                } else {
                    cb(null, results);
                }
            });
        },

推荐答案

当您的catch回调应该处理任何事情时,它将像其他所有promise回调一样需要return该新结果.在您的情况下,它可以返回重试尝试的结果的承诺:

When your catch callback is supposed to handle anything, it will need to return that new result like every other promise callback. In your case, it could return the promise for the result of the retry attempt:

function readDocRetry(id, retries) {
    var self = this;
    return new Promise(function(resolve, reject){
        self.client.readDocumentAsync(self.docsLink + id, null, function(err, data){
            if (err) {
                reject(err);
            } else {
                resolve(data)
            }
        });
    }).then(function(results) {
        console.log("ReadDocRetry result: " + results)
        return results;
    }).catch(function(err, headers) {
        if (err && err.code) {
            if (err.code === 429 && retries >= 0) {
                return Promise.delay(headers['x-ms-retry-after-ms'] || 1).then(function() {
                    return self.readDocRetry(id, retries - 1); 
                });
            } else if (err.code === 503 && retries >= 0) {
                return Promise.delay(500).then(function() {
                    return self.readDocRetry(id, retries - 1) 
                });
            }
        }
        if (err) {
            console.log(err);
            throw err;
        } else {
            console.log("Err missing in RetryError");
            throw new Error("rejection without error");
        }
    });
}

这篇关于蓝鸟承诺重试DocumentDB请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆