jQuery延遲和承諾-.Then()vs.Done()我一直在閱讀關(guān)于jQuery延遲和承諾的文章,我看不出使用.then() & .done()為了成功的回調(diào)。我知道呀艾瑞克·海因茲提到.done()和.success()映射到相同的功能,但我猜也是如此.then()因?yàn)樗谢卣{(diào)都是在一個(gè)成功的操作完成后調(diào)用的。有人能告訴我正確的用法嗎?
3 回答

一只名叫tom的貓
TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
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)})
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貢獻(xiàn)1794條經(jīng)驗(yàn) 獲得超8個(gè)贊
.done()
.then()
.fail()
- 3 回答
- 0 關(guān)注
- 684 瀏覽
添加回答
舉報(bào)
0/150
提交
取消