本文介绍了承诺的第二。然后()不失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在链接 .then()调用一个承诺时遇到问题。在执行以下代码时:

  var prom = new Promise(function(resolve,reject){ 
//让我们总是失败
reject(Error(buuu!!));
});
$ b var thenable =
prom.then(
function(done){
console.log(First handler:Done !: Argument:,done);
returnFirst then:DONE;
},
function(fail){
console.error(First handler:Fail !. Argument:,fail);
returnFirst then:FAIL;
}
).then(
function(done){
console.info(Second handler:Done !. Argument: ,done);
},
function(fail){
console.error(Second handler:Fail !. Argument:,fail);
}
);

这将在控制台中显示以下内容:

 第一个处理程序:失败!参数:错误{stack:(...),message:buuuu!} 
第二句柄:完成!参数:首先:失败



为什么第二个 then()已调用 done 处理程序而不是失败一个?



这是Chrome的错误吗? (请注意,我只对Google Chrome的行为感兴趣)



我是否需要求助于返回预先解析/拒绝的Promise .then 处理程序?

解决方案

因为您在第一个 then(),从而使承诺能够解决您从它返回的First then:FAIL字符串。



您可以这样做,是的。触发第二个失败处理程序的其他方法是:


  • 简单地省略第一个错误处理程序:

      prom.then(function(done){
    console.log(First handler:Done !: Argument:,done);
    returnFirst then:DONE;
    ))。then(function(done){
    console.info(Second handler:Done !. Argument:,done);
    },function(fail){
    console.error(Second handler:Fail !. Argument:,fail);
    });


  • 或重新抛出一个异常(类似于返回被拒绝的promise):


    $ b $

      prom.then(function(done){
    console.log(First handler:Done !: Argument:,done) ;
    returnFirst then:DONE;
    },function(fail){
    console.error(First handler:Fail !. Argument:,fail);
    抛出new Error(First then:FAIL); //或:throw fail;
    //或者,您可以返回一个被拒绝的promise:
    return Promise.reject(new Error(First then: FAIL));
    })。then(function(done){
    console.info(Second handler:Done !. Argument:,done);
    },function ){
    console.error(Second handler:Fail !. Argument:,fail);
    });



I'm having trouble with chaining .then() calls for a promise. When executing the following code:

var prom = new Promise(function(resolve, reject) {
  //let's always fail
  reject(Error("buuuu!"));
});

var thenable = 
prom.then(
    function(done) {
        console.log("First handler: Done!: Argument: ", done);
        return "First then: DONE";
    },
    function(fail) {
        console.error("First handler: Fail!. Argument: ", fail);
        return "First then: FAIL";
    }
).then(
    function(done) {
        console.info("Second handler: Done!.  Argument: ", done);
    },
    function(fail) {
        console.error("Second handler: Fail!.  Argument: ", fail);
    }
);

This prints the following in the console:

First handler: Fail!. Argument:  Error {stack: (...), message: "buuuu!"}
Second handler: Done!.  Argument:  First then: FAIL


Why does the second then() has its done handler called instead of the fail one?

Is this a bug with Chrome? (Note that I'm only interested in Google Chrome's behavior)

Do I need to resort to returning pre-resolved/rejected Promises from the .then handlers?

解决方案

Because you handled the error in the first then() of the chain already, making the promise resolve with the "First then: FAIL" string that you returned from it.

No, that's how promises are supposed to work.

You can do that, yes. Other ways to trigger the second fail handler would be:

  • Simply omit the first error handler:

    prom.then(function(done) {
        console.log("First handler: Done!: Argument: ", done);
        return "First then: DONE";
    }).then(function(done) {
        console.info("Second handler: Done!.  Argument: ", done);
    }, function(fail) {
        console.error("Second handler: Fail!.  Argument: ", fail);
    });
    

  • Or rethrow an exception (that's similar to returning a rejected promise):

    prom.then(function(done) {
        console.log("First handler: Done!: Argument: ", done);
        return "First then: DONE";
    }, function(fail) {
        console.error("First handler: Fail!. Argument: ", fail);
        throw new Error("First then: FAIL"); // or: throw fail;
        // alternatively, you can return a rejected promise:
        return Promise.reject(new Error("First then: FAIL"));
    }).then(function(done) {
        console.info("Second handler: Done!.  Argument: ", done);
    }, function(fail) {
        console.error("Second handler: Fail!.  Argument: ", fail);
    });
    

这篇关于承诺的第二。然后()不失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 15:15