rxjs_1

by Ibrahim Islam

HTML

<script src="https://unpkg.com/@reactivex/rxjs@5.0.0-rc.1/dist/global/Rx.js"></script>

Babel + JSX

var Observable = Rx.Observable;

function xhr(url, json) {
    return Observable.create(function (observer) {
        $.ajax({
            url: url,
            data: JSON.stringify(json),
            success: function (response) {
                observer.next(json);
            },
            error: function (jqXHR, status, error) {
                observer.error(error);
            },
            complete: function () {
                observer.complete();
            }
        });
    });
}

var ids = [1,2,3,4,5,6];
var ids$ = (id) => {
	return Observable.of(id);
};
var loadIds$ = (id) => {
	if(id == 4) return xhr('/echo/jsoneee/', {id: id});
	return xhr('/echo/json/', {id: id});
};

Observable.from(ids)
.concatMap(id => Observable.forkJoin(ids$(id), loadIds$(id)))
.catch((err, caught) => {
	//get the id for which loadIds failed
  //send the id to subscribe() and continue the subscription
  return Observable.empty();
})
.subscribe(result => console.log(result))