0

My currentReq var is set to a call back method:

this.currentReq = cb.apply(this,args);

Later I may wish to abort the method if it is an ajax call.

I know I can use:

this.currentReq.abort();

But what if the currentReq is not an ajax call? How can I test for this and only abort if its an ajax call? I get an error when I try and abort on a standard deferred.

3
  • in general in these situations, you look for a "smoking gun" that only the one you need to target has or has set to a certain value.
    – dandavis
    Commented Jun 22, 2015 at 21:25
  • Related SO question
    – silentw
    Commented Jun 22, 2015 at 21:26
  • mmm... why not just do the classic duck typing thing and see if its abortable? Unless you have other abortable calls that aren't the ajax calls you're looking for.
    – Shashank
    Commented Jun 22, 2015 at 21:31

2 Answers 2

3

You may first test to see if the abort method exists:

if (typeof(this.currentReq.abort) == "function") {
     this.currentReq.abort();
}
2

If the error is to an undefined method the you should be able to test if the function itself exists

if(typeof this.currentReq.abort === 'function') {
    this.currentReq.abort();
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.