使用`resolve`和`reject`方法的`promise.then`方法与使用`if/else`的比较。

huangapple go评论71阅读模式
英文:

promise - then method using resolve reject method vs if/else

问题

我是新手JavaScript,我已经自学了Promise API(异步操作)。但是我对then方法中的结果处理有点困惑,特别是对于拒绝的情况。

以下两个示例等价吗?(我只需要一个解释)

exp1:

promise.then(
  (result) => { 
     console.log(result);
  },
  (error) => { 
     console.log(error);
  }
);

exp2

promise.then(
  (result) => { 
    if (result.ok) {
     console.log(result);
  } else {
     console.log('error');
  }
});

对于exp1then方法使用了两个参数(resulterror),对于exp2只使用了一个参数,但它在同一个块中考虑了成功和失败的情况,result在我看来是已完成的。

英文:

I'm new to javascript and I've self taught the promise API (asynchronous operation). yet I'm a little but confused with result handling in then method especially for rejection.

does the 2 following examples the equivalent ? (I just need an explanation)

exp1:

promise.then(
  (result) => { 
     console.log(result);
  },
  (error) => { 
     console.log(error);
  }
);

exp2

promise.then(
  (result) => { 
    if (result.ok) {
     console.log(result);
  } else {
     console.log('error');
  });

for exp1 there are the use of 2 arguments in then (result & error), for the exp2 only one argument but it is considering success & fail in same bloc result which is imo the fulfilled

答案1

得分: 0

第一个代码部分:

理论上,您正在查看特定的承诺,这些承诺打算用带有ok键的对象解决。例如,这就是fetch()所做的事情。

区别:

  • 第一段代码不检查此ok键的值,因此如果承诺以result.ok等于false解决,那么输出将不同。在这种情况下(当ok为false时),第一段代码将记录result对象(在那里我们将看到okfalse),而第二段代码将记录"error"。

  • 第二段代码不处理拒绝,因此如果承诺拒绝,第一个代码将输出"error",而第二个将引发"未捕获的(在承诺中)TypeError"。

请注意,fetch在其响应中提供的ok键表示它收到了HTTP响应,而拒绝表示存在更基本的错误。

如果您想在ok键为false的情况下执行catch处理程序,请像这样链接承诺:

fetch("https://httpstat.us/405").then(response => {
    if (!response.ok) { // 抛出错误将拒绝链接的承诺:
        throw new TypeError("The 'ok' key is false, and 'status' is " + response.status);
    }
    console.log(response);
}).catch(error => {
    console.log("############### OOPS ######################");
    console.log(error);
});
英文:

Presumably you are looking at specific promises -- ones that intend to resolve with an object that has an ok key. For example, this is what fetch() does.

The differences:

  • The first code does not check the value of this ok key, and so if the promise fulfils with result.ok equal to false, then the output will be different. The first code snippet will in that case (when ok is false) log the result object (where we will see ok is false), the second code snippet will log "error".

  • The second code does not deal with rejections, so that if the promise rejects, the first one will output "error", while the second will raise an "Uncaught (in promise) TypeError".

Note that the ok key that fetch provides in its response is an indication that it received an HTTP response, while a rejection means there was a more fundamental error.

If you want to execute the catch handler in case the ok key is false, then chain promises like this:

fetch("https://httpstat.us/405").then(response => {
    if (!response.ok) { // Throwing an error will reject the chained promise:
        throw new TypeError("The 'ok' key is false, and 'status' is " + response.status);
    }
    console.log(response);
}).catch(error => {
    console.log("############### OOPS ######################");
    console.log(error);
});

huangapple
  • 本文由 发表于 2023年7月20日 20:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729687.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定