Question: I am a publisher that is contracted to serve an ad to only US users. We are serving this ad using geoIP database from MaxMind to identify IP addresses.

Based on DoubleClick metrics, we are showing that of 3200 impressions served, 7% are still being served outside the US (Germany, France, India, Italy, UK with the most impressions).

Is there a better technology to geo-target ads to the US?

Answer: 
Leveraging promises, you could just do it this way. Might not be particularly elegant, but it works quite well and is very readable:

```
var func = function() {
   return $.ajax(...);
};

func() //call 1
  .then(null,function() { return func(); }) //call 2
  .then(null,function() { return func(); }) //call 3
  .then(null,function() { console.log('3 times'); })

```

**EDIT:**

If you are using jQuery less than 1.8, you'll have to change `.then` to `.pipe`

```
var repeat = function(request,times) {
    var r = request();
    while( times > 0) {
       r = r.then(null,function() { return request(); })
       times--;
    }

    return r;
}

repeat($.ajax.bind(null,{url:'https://www.google.com'}),10)
.then(
   function() {
      console.log('success!');
   },
   function() { 
      console.log('failed 10 times'); 
   }
);

```
```
$.ajax({
  url: 'http://api.test.com',
  method: "POST",
  contentType: "application/json; charset=utf-8",
  dataType: "json",
  data: data
  tryCount : 1,
  retryLimit : 3,
  error: function(xhr, textStatus, err){
       if (this.tryCount <= this.retryLimit) {
           console.log("error")
           this.tryCount++;
           $.ajax(this);
       } else {
               console.log("max number ...");
              }
            }
})
.done(function(returnObj) {
  return returnObj;
})
.fail(function(err) {
  console.log(err);
});

```