2

I'm trying to figure out why my use of retry is not working in this example: http://jsbin.com/bobecoluxu/edit?js,output

var response$ = Rx.Observable.fromPromise(
  $.ajax({
    url: 'http://en.wikipedia.org/w/api.php',
    dataType: 'jsonp',
    data: {
      action: 'opensearch',
      format: 'json',
      search: term
    }
  }))
.retry(3);

I've wrapped the ajax call in an Observable in the searchWikipedia function, but if I try to force the failure of this call by turning off the wifi or throwing an exception by the related operator it simply doesn't work.

Thanks in advance!

Calvin Belden
  • 3,114
  • 1
  • 19
  • 21
Brakko
  • 43
  • 1
  • 7
  • Please insert your code **directly in the question itself**. Links can change and be outdated, invalidating your question. – unbindall Mar 17 '16 at 22:49
  • @scriptHero, ok sorry for the incovenient question text and thanks to fixed it for me. – Brakko Mar 18 '16 at 06:25

1 Answers1

1

When you pass a promise to fromPromise and call retry, it will simply keep emitting the same Promise (ie subsequent HTTP requests won't be made).

If you pass a function that returns a Promise to fromPromise, that function will be re-invoked (allowing subsequent HTTP requests to be sent upon failure). The following example illustrates this:

const makesPromise = () => {
    console.log('calling');

    // Purposefully reject the Promise. You would return the return value
    // of your call to $.ajax()
    return Promise.reject();
};

const stream = Rx.Observable.fromPromise(makesPromise).retry(3);

stream.subscribe(log);

// >> calling
// >> calling
// >> calling
// Finally throws an uncaught error

Note: I had to update to the latest 4.x release of RXJS to use this feature

Calvin Belden
  • 3,114
  • 1
  • 19
  • 21
  • 2
    Another way of phrasing this is that you have not given RxJS a way to create the promise/issue the request, so it has no idea how to do so when it goes to retry. Instead, you have made the request for it and given it the result (a promise) only. It's like the difference between giving a man a fish, and teaching him how to fish. If you give someone a fish, and it turns out to be off, but you are nowhere in sight, the best they can do is hope for you to come back with a new, fresh fish. – GregL Mar 18 '16 at 00:07