Redux Observable:如何在promise中返回一个动作?

时间:2017-04-20 07:52:36

标签: rxjs redux-observable

我有一部史诗:

export default function uploadImage(action$, store) {
  return action$.ofType(userActions.UPLOAD_IMAGE)
    .mergeMap(action => {
      ...
      RNFetchBlob.fs.readFile(uploadUri, 'base64').then(data => {
        firebaseRef.putString(data).then(snapshot => {
          console.log('Uploaded a blob or file!');
          return Observable.of(userActions.updateImageURL(snapshot.downloadURL)); //<----------
        })
      });
    })
};

但是,这会使You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.有理由抛出。

如何将回调包装在一个observable中,以便epic不会中断?

2 个答案:

答案 0 :(得分:2)

除了Maxime回答之外,在你的例子中看来实际问题是你并没有真正回归Promise!您需要返回它或切换到使用箭头的隐式返回语法

export default function uploadImage(action$, store) {
  return action$
    .ofType(userActions.UPLOAD_IMAGE)
    .mergeMap(action => {
      return Observable // don't forget to return this chain!
        .fromPromise(RNFetchBlob.fs.readFile(uploadUri, 'base64'))
        .map(data => {
          firebaseRef.putString(data).then(snapshot => {
            console.log('Uploaded a blob or file!');
            return userActions.updateImageURL(snapshot.downloadURL);
          })
        });

这是一个非常常见的错误,我们都去过那里!

答案 1 :(得分:1)

可能是这样的:

export default function uploadImage(action$, store) {
  return action$
    .ofType(userActions.UPLOAD_IMAGE)
    .mergeMap(action => {
      ...
      Observable
        .fromPromise(RNFetchBlob.fs.readFile(uploadUri, 'base64'))
        .map(data => {
          firebaseRef.putString(data).then(snapshot => {
            console.log('Uploaded a blob or file!');
            return userActions.updateImageURL(snapshot.downloadURL);
          })
        });