在承诺中,当时和最后有什么区别? [英] What is the difference between then and finally in a promise?
问题描述
我看到Bluebird的 最后
的文档但我仍然不太明白与然后
的区别。
I see the docs for Bluebird's finally
but I still don't quite understand the difference vs. then
.
要明确:我确切地知道为什么然后
在 catch
之后被调用。我希望在捕获后调用它。这是意图。我的问题是:如果我希望代码始终执行而不管承诺状态如何,那么然后
vs 之间的区别是什么?最后
为此?
To be clear: I know exactly why then
gets called after a catch
. I want it to be called after a catch. That's the intent. My question is: If I want code to always be executed regardless of the promise state, what's the difference between then
vs finally
for that?
我建立了这个测试:
var Promise = require("bluebird");
function test1 () {
console.log("RESOLVE + THEN + CATCH + THEN");
return new Promise((resolve, reject) => resolve())
.then(() => console.log("then"))
.catch(err => console.log("error:", err.message))
.then(() => console.log("end"));
}
function test2 () {
console.log("REJECT + THEN + CATCH + THEN");
return new Promise((resolve, reject) => reject(new Error("rejected")))
.then(() => console.log("then"))
.catch(err => console.log("error:", err.message))
.then(() => console.log("end"));
}
function test3 () {
console.log("RESOLVE + THEN + CATCH + FINALLY");
return new Promise((resolve, reject) => resolve())
.then(() => console.log("then"))
.catch(err => console.log("error:", err.message))
.finally(() => console.log("end"));
}
function test4 () {
console.log("REJECT + THEN + CATCH + FINALLY");
return new Promise((resolve, reject) => reject(new Error("rejected")))
.then(() => console.log("then"))
.catch(err => console.log("error:", err.message))
.finally(() => console.log("end"));
}
// run tests "sequentially" so console output doesn't get blended
setTimeout(test1, 500);
setTimeout(test2, 1000);
setTimeout(test3, 1500);
setTimeout(test4, 2000);
这测试四种情况:
-
.then(...)。catch(...)。then(...)
带有已解决的承诺。 -
.then(...)。catch(...)。then(...)
被拒绝的承诺。 -
.then(...)。catch(...)。finally(...)
带有已解决的承诺。 -
.then(...)。catch(...)。finally(...)
被拒绝的承诺。
.then(...).catch(...).then(...)
with a resolved promise..then(...).catch(...).then(...)
with a rejected promise..then(...).catch(...).finally(...)
with a resolved promise..then(...).catch(...).finally(...)
with a rejected promise.
我看到的结果是案例1 + 2的行为与3 + 4相同:最后一位(然后
或最后
取决于测试)无论发生什么事情都按预期执行。该程序的输出是:
The results I see are cases 1+2 behaves identically to 3+4: The last bit (then
or finally
depending on test) executes no matter what happens before it, as intended. The output of that program is:
RESOLVE + THEN + CATCH + THEN
then
end
REJECT + THEN + CATCH + THEN
error: rejected
end
RESOLVE + THEN + CATCH + FINALLY
then
end
REJECT + THEN + CATCH + FINALLY
error: rejected
end
现在,原因我'问是因为我看到了对此发表评论我提出的其他问题:
Now, the reason I'm asking is because I saw a comment on this other question I asked:
不确定你的承诺是否支持它,但你应该改变最后的
.then
到.finally
以便busy
总是被清除。
根据我对然后
的非常有限的了解,以及上面的测试,它似乎是然后
就足够了。但在那次评论之后,我正在质疑自己以及使用然后
执行最终代码的安全性。
From my very limited knowledge of then
, and the tests above, it seems like then
is sufficient. But after that comment I'm questioning myself and the safety of using then
to execute "finally" code.
所以我的问题是:然后
和最终
之间有什么区别?他们看起来就像他们的行为一样,但我什么时候需要使用 finally
而不是然后
?
So my question is: What's the difference between then
and finally
? They look like they behave the same, but when would I need to use finally
instead of then
?
推荐答案
第一个区别:有时你不想在它们出现的地方发现错误,但在代码中使用此功能,所以你不要抓住它们。在这种情况下,你不能替换然后()
和 finally()
。
First difference: Sometimes you don't want to catch errors at the place they arise, but in the code that uses this function, so you don't catch them. In that case you can't substitute then()
and finally()
.
有时你必须清理一些事情是否有错误(归零引用,清除超时......这样的东西)。这就是你使用 finally()
的地方。
Sometimes you have to clean something up whether there was an error or not (nulling references, clearing timeouts ... stuff like that). That's where you use finally()
.
第二个区别:传递给 catch()
的函数也可能抛出,那么你会被拒绝承诺并且不会调用以下 then()
。
Second difference: The function you pass to catch()
could also throw, then you would have a rejected Promise and the following then()
would not be called.
(所以最终在一个catch仍然会执行错误之前,不知道那个)
(so a finally before a catch will still execute on an error, didn't know that)
是的,这是最后()
。它将在任何情况下执行,而不会更改已解决的值。
Yeah, that's the point of finally()
. It will be executed under any circumstance without changing the resolved value.
您可能希望阅读/ google一下 try {} finally {}
,没有捕获。
You might want to read/google a bit about try {} finally {}
, without catch.
这篇关于在承诺中,当时和最后有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!