1function doTheThing() {
2 return new Promise((resolve, reject) => {
3 $.ajax({
4 url: window.location.href,
5 type: 'POST',
6 data: {
7 key: 'value',
8 },
9 success: function (data) {
10 resolve(data)
11 },
12 error: function (error) {
13 reject(error)
14 },
15 })
16 })
17}
1doTheThing()
2 .then((data) => {
3 console.log(data)
4 doSomethingElse()
5 })
6 .catch((error) => {
7 console.log(error)
8 })
1function ajax(options) {
2 return new Promise(function (resolve, reject) {
3 $.ajax(options).done(resolve).fail(reject);
4 });
5}
1function doTheThing() {
2 $.ajax({
3 url: window.location.href,
4 type: 'POST',
5 data: {
6 key: 'value',
7 },
8 success: function (data) {
9 console.log(data)
10 },
11 error: function (error) {
12 console.log(error)
13 },
14 })
15}
1 $.ajax({
2 url: "/someurl",
3 method: "GET",
4 data: {
5 a: "a"
6 })
7 .done(function(data) {
8 console.log('success callback 1', data)
9 })
10 .done(function(data) {
11 console.log('success callback 2', data)
12 })
13 .fail(function(xhr) {
14 console.log('error callback 1', xhr);
15 })
16 .fail(function(xhr) {
17 console.log('error callback 2', xhr);
18 });
19