jquery - How to report an additional reject from inside Ajax done promise -
i looking best way prepare situation. need additional checks of returned ajax info cause _loadpanel
promise operation "fail":
_loadpanel(url: string, data?: any) : jquerypromise<any> { homecoming $.ajax( { url: url, type: info ? "post" : "get", data: info }).done(function (html: string, textstatus: string) { // function exracts required info var $panel = extractpaneldata(html); if (!$panel.length) { // how cause reject here??? } }); }
you can't (below) promise returned ajax immutable , smells wrong anyway:
var promise = $.ajax({...}).done(function(...) { promise.reject("data failed promise"); });
is there simple way of doing not involve adding $.deferred()
object or pattern?
i have tried following, modify parent deferred
object not compile:
var dfd = $.deferred(); var promise = dfd.promise(); promise = promise.then($.ajax( { cache: false, url: url, type: info ? "post" : "get", data: info }).done(...));
it allow me combine ajax
promise deferred
promise
supplied parameters not match signature of phone call target:type '(value: {}) => {}' requires phone call signature, type 'jqueryxhr' lacks one.
suggestions?
something cool:not sure how known is, , not help problem (as returned immutable promise), appears can utilize $.when
pass initial parameters first function
e.g. passing initial parameters $.ajax
this:
$.when({ url: url, type: data? "post" : "get", data: info }).then($.ajax).done(function (html: string, textstatus: string)
or separate parameters this:
$.when("hello!", 22, "there!") .then(insertpanel);
this can have interesting benefits if result chained previous promises ajax phone call not longer run immediately, after previous steps complete.
there few thigns here:
.then
chains promises , changes state. .done
not chain promises, adds handler current promise , returns it. so:
var xhr = $.ajax({...}); p = xhr.then(function(data){ // fail if check fails if(additionalchecksfail) homecoming $.deferred.reject(error("invalid request").promise(); homecoming data; // otherwise maintain same info }); p.then(function(data){ // run if info validation passed. });
jquery typescript promise
No comments:
Post a Comment