可以观察到2个承诺

时间:2018-03-06 03:54:34

标签: redux rxjs

我认为我的解决方案就是这个问题,但我无法让它工作Promise.all behavior with RxJS Observables?

我试图通过forkJoin返回两个promises上的observable。

一个承诺从服务器获取一个ID,另一个承诺处理文件以生成缩略图。

export function createSceneFromFile(action$) {
  return action$.ofType(CREATE_SCENE_FROM_FILE)
    .mergeMap(({locationId,file}) =>
      createSceneThumb(locationId,file)
        .map((res,preview) => {
          console.log(res,preview)
          if (res.error) {
            return { type: CREATE_SCENE_FAILED, payload: res.message }
          } else {
            return {type: CREATE_SCENE_SUCCESS, payload:  {...res.payload,preview} }
          }
        })
        .catch(err => { return { type: CREATE_SCENE_FAILED, message: err } })
    )
}

function createSceneThumb(locationId,file){

  const request = fetch(`${API_URL}/location/${locationId}/createscene/${file.name}/`, {
    method: 'get',
    credentials: 'include',
  }).then(res => res.json())

  const thumb = fileToScenePreview(file)

  return Observable.forkJoin(request,thumb)
}


export function fileToScenePreview(file){
  return new Promise((resolve,reject)=>{
    getFileThumb(file).then((canvas)=> {
      canvas.toBlob((blob) => {
        blob.lastModifiedDate = new Date()
        blob.name = 'scenepreview_' + file.name + '.png'

        const uploader = new S3Upload({
          getSignedUrl: getSignedUrl,
          uploadRequestHeaders: {'x-amz-acl': 'public-read'},
          contentType: 'image/png',
          scrubFilename: (filename) => filename.replace(/[^\w\d_\-.]+/ig, ''),
          contentDisposition: 'auto',
          s3path: 'assets/',
          onError:()=>reject,
          onFinishS3Put: ()=>resolve(blob.name),
        })
        uploader.uploadFile(blob)
      })
    })
  })
}

但我从未得到回应。

这是正确的方式吗?

0 个答案:

没有答案