q 承诺链中 "done" 的目的是什么?
What is the purpose of "done" in q promise chains?
在 Q
承诺链中使用 done
的目的是什么。 IIUC 它传播错误。如果是这样,我怎样才能发现所说的错误?
以下代码输出:Uncaught Error: hello world
。我怎样才能捕捉到这个错误?我 有 来使用 .catch
语法,还是有其他方法?
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/q.js/0.9.2/q.js"></script>
</head>
<body>
</body>
<script>
try {
var foo = {
bar: function() {
var d = Q.defer();
d.resolve();
return d.promise;
}
};
function bam() {
throw new Error('hello world');
}
foo.bar().then(bam).done(); // what is the purpose of done?
} catch (e) {
alert(e);
}
</script>
</html>
正如源代码中的文档评论所说:
Terminates a chain of promises, forcing rejections to be thrown as exceptions.
在 Q
承诺链中使用 done
的目的是什么。 IIUC 它传播错误。如果是这样,我怎样才能发现所说的错误?
以下代码输出:Uncaught Error: hello world
。我怎样才能捕捉到这个错误?我 有 来使用 .catch
语法,还是有其他方法?
<html>
<head>
<script src="http://cdnjs.cloudflare.com/ajax/libs/q.js/0.9.2/q.js"></script>
</head>
<body>
</body>
<script>
try {
var foo = {
bar: function() {
var d = Q.defer();
d.resolve();
return d.promise;
}
};
function bam() {
throw new Error('hello world');
}
foo.bar().then(bam).done(); // what is the purpose of done?
} catch (e) {
alert(e);
}
</script>
</html>
正如源代码中的文档评论所说:
Terminates a chain of promises, forcing rejections to be thrown as exceptions.