/ / $ .जैक्स वादे 'तब नहीं' जैसा कि मैं उम्मीद कर रहा हूँ - jquery, jquery-deferred

$ .ajax वादा करता है 'तो' मैं उम्मीद नहीं कर रहा हूँ - jquery, jquery-deferred

नीचे मेरा कोड है। पहला कंसोल संदेश परिणाम दिखाता है, दूसरा एक "t ... doesn" t यह जादुई रूप से तोड़ने का वादा करता है ... यह कैसे संभव है?

    this.saveAsTemplate = function(name, asNew) {
return _saveSet(name, asNew)
.then(function(result) {
// -> result is set correctly
console.log("oh but youll ****ing work... wth?", result);
$.ajax({
url: "/rest/filter/template/"+result.id,
type: "PUT",
}).success(function(result) {
console.log("successfully saved template, result: ", result);
});
})
.then(function(result) {
// -> result is undefined :(
console.log("no ****ing result: ", result);
});

पिछले कुछ महीनों से मुझे इस मुद्दे को हल करने के लिए अतिरिक्त डिफरेन्स लिखना पड़ रहा है ... यह वास्तव में मेरे कोड के साथ खिलवाड़ है। मदद की बहुत सराहना की जाएगी!

संपादित करें: ठीक से जंजीरों के वादों का एक अच्छा स्पष्ट उदाहरण इस प्रश्न में पाया जा सकता है: (नोटिस सब कुछ समारोह रिटर्न) मैं jQuery वादों का उपयोग करते हुए तीन अतुल्यकालिक कॉल श्रृंखला कैसे बना सकता हूं?

उत्तर:

उत्तर № 1 के लिए 4

के लिये .then इस तरह से श्रृंखला के लिए, आपको इसके लिए एक वादा वापस करना होगा।

return $.ajax({

पूरा नमूना:

this.saveAsTemplate = function(name, asNew) {
return _saveSet(name, asNew).then(function(result) {
console.log("oh but youll ****ing work... wth?", result);
// *** The following line was modified ***
return $.ajax({
url: "/rest/filter/template/"+result.id,
type: "PUT",
}).done/*success*/(function(result) {
console.log("successfully saved template, result: ", result);
});
}).then(function(result) {
console.log("no ****ing result: ", result);
});