jQuery延迟和承诺-.Then()vs.Done()我一直在阅读关于jQuery延迟和承诺的文章,我看不出使用.then() & .done()为了成功的回调。我知道呀艾瑞克·海因兹提到.done()和.success()映射到相同的功能,但我猜也是如此.then()因为所有回调都是在一个成功的操作完成后调用的。有人能告诉我正确的用法吗?
3 回答
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
donethen
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)})abc123undefined
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.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 of return statement in above then})// Parallel http requestspromise.then(function (x) { // Suppose promise returns "abc"
console.log(x);
var promise1 = $http.get('/some/data?value=xyz').then(function (result) {
console.log(result); // suppose result === "xyz"
return result;
});
var promise2 = $http.get('/some/data?value=uvm').then(function (result) {
console.log(result); // suppose result === "uvm"
return result;
});
return promise1.then(function (result1) {
return promise2.then(function (result2) {
return { result1: result1, result2: result2; }
});
});}).then(function (result){
console.log(result); // result === { result1: 'xyz', result2: 'uvm' }}).then(function (und){
console.log(und) // und === undefined, because of absence of return statement in above then})// Sequential http requestspromise.then(function (x) { // Suppose promise returns "abc"
console.log(x);
return $http.get('/some/data?value=xyz').then(function (result1) {
console.log(result1); // suppose result1 === "xyz"
return $http.get('/some/data?value=uvm').then(function (result2) {
console.log(result2); // suppose result2 === "uvm"
return { result1: result1, result2: result2; };
});
});}).then(function (result){
console.log(result); // result === { result1: 'xyz', result2: 'uvm' }}).then(function (und){
console.log(und) // und === undefined, because of absence of return statement in above then})
幕布斯7119047
TA贡献1794条经验 获得超8个赞
.done()
.then()
.fail()
- 3 回答
- 0 关注
- 704 浏览
添加回答
举报
0/150
提交
取消
