ES6中的捕获错误有望实现

时间:2015-05-11 10:46:50

标签: javascript node.js promise ecmascript-6

有没有人对ES6承诺有任何想法,我在我的Node应用程序中使用它们,我喜欢它们,大多数情况下。但是我发现,如果我在一个解决回调中遇到某种错误,它就不会抛出错误或执行拒绝回调,而是让我的服务器无限挂起。

现在我已经采取了这种做法,并手动拒绝了捕获错误的承诺,但我不确定这是否是一个很好的处理方式,和/或我是否应该使用promises一点都不。

this.dataStore.set(newID, value).then( (foo) => {
    try{
        this.var = foo;
        res({val: foo});
    }catch(e){
        rej(e);
    }
}, (e) => {
    rej(e);
});

1 个答案:

答案 0 :(得分:4)

我认为混淆的原因在于,基于您在此使用resrej,您可能会在承诺构造函数中调用此函数,而不是

function setStore(newID, value) {
    return new Promise(function(res, rej) {
        this.dataStore.set(newID, value).then( (foo) => {
            try{
                this.var = foo;
                res({val: foo});
            }catch(e){
                rej(e);
            }
        }, (e) => {
            rej(e);
        });
    });
}

顺便说一下,最后的(e) => { rej(e); }部分可以重写为e => rej(e),而rej又可以重写为this.dataStore.set

但无论如何,你不需要任何周围的机制来创建和回复你自己的承诺,因为then和/或随后对then 的呼叫已经创建一个您可以按原样返回的承诺。而不是创建自己的新承诺,然后使用基于传递给dataStore.set的结果的小哈希来解析新承诺,只需返回哈希 - 这将成为结果承诺。当function setStore(newID, value) { return this.dataStore.set(newID, value).then(foo => { this.var = foo; return {val: foo}; }); } 的调用失败时,不要拒绝你的新承诺,而是让失败的承诺本身。

所以你不应该做比

更复杂的事情
this.var = foo; return {val: foo};

this.dataStore.set部分发生错误(但怎么可能?)会自动将承诺置于失败状态。由setStore('abc', 123) .then(hash => console.log(hash.val)) .catch(e => console.log("The sky is falling", e)); 导致的失败将导致失败的承诺,并且没有必要捕获它并重新抛出它 - 失败将在链中快速进行。

将此用作:

promise.then(success, failure)

作为澄清问题,在以下内容中:

success

failure回调中success回调中出现的错误处理。 success回调中的失败将在链的连续阶段中处理。您可以使用

处理promise(或promise.then(success).catch(failure) 本身)中的失败
private void CharsAvailable()
        {
            int num = 0;
            int num2 = 0;
            string text = this.txtMessage.Text;
            if (this.chkSignature.Checked)
            {
                text = text + Environment.NewLine + this.txtSignature.Text;
            }
            else
            {
                text = this.txtMessage.Text;
            }
            int num3 = 0;
            while (num != 1 && num3 < this.txtMessage.TextLength)
            {
                if (text[num3] < '0' || text[num3] > '9')
                {
                    if (text[num3] < 'A' || text[num3] > 'Z')
                    {
                    }  
                 ..............................
                   SOME CODE 
                 ............................

                if (num == 1)
                {
                    int num4 = text.Length;
                    if (num4 <= 70)
                    {
                        num4 = 1;
                    }
                    else
                    {
                        num4 += 62;
                        num4 -= num4 % 63;
                        num4 /= 63;
                    }
                    frmCompose.isUnicodeFound = true;
                    this.lblChar.Text = string.Concat(new object[]
                    {
                        text.Length,
                        " characters, ",
                        num4,
                        " SMS message(s)"
                    });
                }
                else
                {
                    int num4 = text.Length + num2;
                    if (num4 <= 160)
                    {
                        num4 = 1;
                    }
                    else
                    {
                        num4 += 152;
                        num4 -= num4 % 153;
                        num4 /= 153;
                    }
                    frmCompose.isUnicodeFound = false;
                    this.lblChar.Text = string.Concat(new object[]
                    {
                        text.Length + num2,
                        " characters, ",
                        num4,
                        " SMS message(s)"
                    });
                }
            }

            //validation

                        if (this.txtMessage.Text.Trim().Length == 0)
                        {
                            MessageBox.Show("Blank messages cannot send. Please type the message.", "Blank Message");
                        }
                        else if (frmCompose.isUnicodeFound && this.cmbLanguage.SelectedIndex == 0)
                        {
                            MessageBox.Show("Please choose 'Unicode' in Language dropdown to send message(s) in non English.”", "Unicode Message");
                        }enter code here
相关问题