gpt4 book ai didi

angularjs - Angular js 等效于 q 库中的完成函数

转载 作者:行者123 更新时间:2023-12-04 23:49:13 25 4
gpt4 key购买 nike

我用过 node.js q 库,有 .done()无论成功或失败,最后都会调用该函数。

我研究但找不到,有人能指出正确的方向吗?

最佳答案

您将使用 .catch(function(e){ throw e; })终止一个链。

什么.done Q 中的作用是终止一个 promise 链,以便抛出未处理的拒绝。由于 $q 使用 $exceptionHandler对于抛出的拒绝(相对于返回的拒绝),执行 .catch(function(e){ throw e; })将达到同样的目的。
.finally相当于 .finally在 Q 中,它的作用与在 $q 中的完全相同。

在 Q 中,做:

Q().then(handle).done(handleSuccess, handleFail);

与以下内容几乎相同:
Q().then(handle).then(handleSuccess, handleFail).done();

这与以下内容非常相似:
$q.when().then(handle).then(handleSuccess, handleFail).catch(function(e){ throw e; });

然而,由于 $q 委托(delegate)给 $exceptionHandler在抛出异常的情况下(与 Q 或其他 promise 库不同)执行 .done不太需要,因为意外链终止的可能性要小得多。为了显示,
Q().then(function(){ return JSON.parse("invalidJson"); });

是 Q 中的吞噬错误,但在 $q 中,
$q.when().then(function(){ return JSON.parse("invalidJson"); });

这会导致控制台中出现大量红色警告(默认情况下)。

关于angularjs - Angular js 等效于 q 库中的完成函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26813571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com