Last active
November 12, 2021 02:54
-
-
Save Tusko/dfe1fcbbb71fc0a36c86cdd79530d1fc to your computer and use it in GitHub Desktop.
$.ajax Promise
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* ajax Defaults (optional): | |
$.ajaxSetup({ | |
type : 'POST', | |
dataType : 'json', | |
cache : true, | |
global : true, | |
data : {}, | |
contentType : 'application/json', | |
beforeSend : function (xhr) { | |
xhr.setRequestHeader('X-Token', 'MY-API-TOKEN'); | |
} | |
}); | |
*/ | |
function ajax(options) { | |
return new Promise(function (resolve, reject) { | |
$.ajax(options).done(resolve).fail(reject); | |
}); | |
} | |
then run: | |
ajax({ | |
url: YOUR_URL, | |
type: 'post', | |
dataType: 'json', | |
data: { | |
action: some_action, | |
action_2: another_action | |
} | |
}).then( | |
function fulfillHandler(data) { | |
// callback | |
} | |
).catch(function errorHandler(error) { | |
// error | |
}); |
i have the query that I am using for loop and from there I am calling the function that will run the ajax. but problem is that my whole loop gets executed not one by one and due to this , at php side the check code not work. But if i make async:false code works fine. Any solution for this problem ??
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
thanks, its works !