Skip welcome & menu and move to editor
Welcome to JS Bin
Load cached copy from
 
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://unpkg.com/@reactivex/rxjs@5.0.3/dist/global/Rx.js"></script>
</body>
</html>
 
const SUBMIT_FORM = 'SUBMIT_FORM';
const ON_FORM_SUBMIT_SUCCESS = 'ON_FORM_SUBMIT_SUCCESS';
const ON_FORM_SUBMIT_ERROR = 'ON_FORM_SUBMIT_ERROR';
const EMPTY_FORM_STATE = 'EMPTY_FORM_STATE';
Rx.Observable.of({type: SUBMIT_FORM})
  .mergeMap(action => mockAjax(action))
  .map(response => formSavedAction(response))
// Notice that catch is now returning the value by dropping the brackets.
  .catch(err => Rx.Observable.of({ type: ON_FORM_SUBMIT_ERROR }))
// send whatever action has come through, and add the empty state action after it.
  .mergeMap(action => Rx.Observable.of(action, { type: EMPTY_FORM_STATE }))
  .subscribe(action => console.log(`dispatch: ${JSON.stringify(action)}`))
function formSavedAction(response) {
  return {type: ON_FORM_SUBMIT_SUCCESS, payload: response}
}
function mockAjax(action) {
  return Rx.Observable.timer(1000).mapTo({})
}
Output

This bin was created anonymously and its free preview time has expired (learn why). — Get a free unrestricted account

Dismiss x
public
Bin info
anonymouspro
0viewers