Poniżej znajduje się mój kod. Pierwszy komunikat na konsoli pokazuje wynik, drugi nie robi ... Czy to nie magicznie łamie obietnice ... jak to jest możliwe?
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);
});
Przez ostatnie kilka miesięcy musiałem pisać dodatkowe odroczenia tylko po to, by obejść ten problem ... to naprawdę psuje mój kod. Pomoc byłaby bardzo ceniona!
EDYCJA: W tym pytaniu można znaleźć dobre, wyraźne przykładowe rozwiązanie odpowiednich łańcuchów: (zauważ, że funkcja zwraca wszystkie funkcje) Jak mogę połączyć trzy asynchroniczne połączenia za pomocą obietnic jQuery?
Odpowiedzi:
4 dla odpowiedzi № 1Dla .then
aby łączyć się w ten sposób, musisz zwrócić mu obietnicę.
return $.ajax({
pełna próbka:
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);
});