0

if the ajax request has not been returned successfully in the first time,i want to ignore the second request until the first request returns successful response.

public static POST<T>(url: string, data): Promise<T> {
    var p = new Promise((resolve, reject) => {
        $.ajax({
            url: url,
            data: data,
            type: "POST",
            cache: false,
            success: function (response: HttpResponse) {
                   //success
            },
            error: function (response) {
                reject("error");
            }
        });
    });

    p.catch((err) => {
        //error
    });

    return p;
}

I solved this problem already.The code as follows:

var flag: { Url: string, Data: any }[];
flag = [];
public static POST<T>(url: string, data): Promise<T> {
  for (var i = 0; i < flag.length; i++) {
        if (flag[i].Url == url && CompareObjectEqual(flag[i].Data, data)) {
            return;
        }
    }
    var postInfo = {
        Url: url,
        Data: data
    };
    flag.push(postInfo);
  var p = new Promise((resolve, reject) => {
    $.ajax({
        url: url,
        data: data,
        type: "POST",
        cache: false,
        success: function (response: HttpResponse) {
               flag = [];
               //success
        },
        error: function (response) {
            reject("error");
        }
    });
  });

  p.catch((err) => {
    flag = [];
    //error
  });

  return p;
}

The function of CompareObjectEqual:

function CompareObjectEqual(a, b) {
  if (!(a instanceof Object && b instanceof Object)) {
    return a === b;
  }

  var aProps = Object.getOwnPropertyNames(a);
  var bProps = Object.getOwnPropertyNames(b);

  if (aProps.length != bProps.length) {
    return false;
  }

  for (var i = 0; i < aProps.length; i++) {
    var propName = aProps[i];
    if ((a[propName] instanceof Object) && (b[propName] instanceof Object)){
        CompareObjectEqual(a[propName], b[propName]);
    } else {
        if (a[propName] !== b[propName]) {
            return false;
        }
    }
  }

  return true;
}
foxery
  • 1
  • 3
  • and how to judge if the two requests are the same.if same,to prevent – foxery Jun 27 '17 at 03:05
  • It seems like you want the second method to be fired after the first. In that case, why not use the `done()` or then `methods()` [More details](https://stackoverflow.com/questions/5436327/jquery-deferreds-and-promises-then-vs-done). You can even call Post from the success method as well. – Nikhil Mathew Jun 27 '17 at 04:12

1 Answers1

0

I'm not certain how your are calling the promise, but you can always use a flag or variable to signal that a request has been sent and then toggle that flag / variable when the response is returned. Also, I assume you are using Typescript with this so I wrote my response using that syntax.

postSubmitted = false;

public static POST<T>(url: string, data): Promise<T> {

    if (postSubmitted) { return };
    postSubmitted = true;

    var p = new Promise((resolve, reject) => {
        $.ajax({
            url: url,
            data: data,
            type: "POST",
            cache: false,
            success: function (response: HttpResponse) {
                postSubmitted = false;
                   //success
            },
            error: function (response) {
                postSubmitted = false;
                reject("error");
            }
        });
    });

    p.catch((err) => {
        //error
    });

    return p;
}
Jason Lemrond
  • 407
  • 2
  • 7