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 關注
- 695 瀏覽
添加回答
舉報
0/150
提交
取消
