我一直在阅读有关jQuery递归和诺言的文章,但看不到使用.then()和.done()进行成功回调的区别。 我知道Eric Hynds提到.done()和.success()映射到相同的功能,但是我猜.success()也是这样.then()因为所有回调都在成功完成操作时被调用。
有人可以启发我正确使用吗?
#1楼
返回结果的处理方式也有所不同(称为链接, done不链接, then生成调用链)
promise.then(function (x) { // Suppose promise returns "abc"
console.log(x);
return 123;
}).then(function (x){
console.log(x);
}).then(function (x){
console.log(x)
})
将记录以下结果:
abc
123
undefined
而
promise.done(function (x) { // Suppose promise returns "abc"
console.log(x);
return 123;
}).done(function (x){
console.log(x);
}).done(function (x){
console.log(x)
})
将得到以下内容:
abc
abc
abc
----------更新:
顺便说一句。 我忘了提一下,如果您返回一个Promise而不是原子类型值,则外部诺言将等到内部诺言得到解决:
promise.then(function (x) { // Suppose promise returns "abc"
console.log(x);
return $http.get('/some/data').then(function (result) {
console.log(result); // suppose result === "xyz"
return result;
});
}).then(function (result){
console.log(result); // result === xyz
}).then(function (und){
console.log(und) // und === undefined, because of absence o