英文:
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');
}
});
对于exp1,then
方法使用了两个参数(result
和error
),对于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
对象(在那里我们将看到ok
为false
),而第二段代码将记录"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 withresult.ok
equal tofalse
, then the output will be different. The first code snippet will in that case (whenok
is false) log theresult
object (where we will seeok
isfalse
), 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);
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论