javascript - jQuery Recursive AJAX Call Promise -
I'm still trying to find out how to use jQuery's deferred object in recursive AJAX calls. I have such code
function request (page, items) {// jsfild build ajax return value for dummy AJAX endpoint var ret = {totalPage: 10, current page: page, item: []}; (Var i = page; i <(page + 5); i ++) {ret.items.push (i); } // AJAX $ .jax (/ url: '/ echo / json /', method: 'POST', data type: 'jason', data: {delay: 1, JSON: JSON.stringify (ret)} calling, Success: Tasks (Data) {if (Data Courent Page & lt; = data.totalPage) {var filtered = Data.im. Filler (function (L) {return L% 2 == 1;}); var newitems = items ConSat (filtered); Console der (nuites); Requests (Data Courent Page + 1, Nuitimes);} and {Console Dir (Item); // Resolve All Items}}}); } Work Request All () {Request (1, []); // There was a promise that all items should be returned}
Here is JSFiddle
I know how to use the promise in the same AJAX call, but I do not know how to use it in a recursive AJAX call I want to call the requestAll
function in a way like the following
var promise = requestAll ); Promise .Done (function (item) {console.dir (item);});
How can I do this?
You should not use if you want to work with promise, then success < / Code> Parameter Instead, you want to make a promise to
return
, and you can use then
to change the outcome of the promise to something else possibly. Want to
function request (page) {... // AJAX promise return $. Ajax ({url: '/ echo / json /', method: 'post', data type: 'jason', data: {delay: 1, json: JSON.stringify (ret)}}); } Function RequestAdsFomos (Page, Item) {Return Request (Page). Then (function (data) {if data (current.pagepage.php); data.totalPage) {return item;} else {var filtered = data.items.filter (function (L) {return l% 2 == 1;}) ; Return RequestOudsform (data coupon page + 1, items.cacket (filtered))}}}; } Request function () {return requestads forms (1, []); } RequestAll () Then (function (item) {console.dir (item);});
Comments
Post a Comment