【发布时间】:2021-11-05 09:26:41
【问题描述】:
我已经研究过 SO 并发现了许多类似的问题/答案,但在我对如何使用此堆栈的基本理解中,我可能遗漏了一些东西。
我正在与 RXJS/obervables 一起开发一个 react native 项目。在某些时候我做文件下载,这部分不是问题。预先存在的axios-rxjs 和react-native-file-system 的组合让我到达我想要的地方。问题是我不确定如何在没有 async/await 的情况下干净地处理它,我知道这是一种反模式。
我想把这个工作代码转换成一个干净的 obervable 风格的流程。
我已经实现了一个 Epic 来执行我想要的操作:
const myDownloadEpic = (
action$,
state$
) =>
action$.pipe(
ofType(myDownloadActionType), // catches the relevant action
map(action => action.payload),
mergeMap(payload =>
downloadManager // a class with the relevant utils to get files
.download(payload), // this axios call returns my Blob file as Observable<Blob>
.pipe(
mergeMap(async response => {
// a promise is returned by RNFS to read the contents of a folder
const files = await RNFS.readDir(RELEVANT_TARGET_PATH)
...
// a promise returned from a custom function that converts my blob to bas64
const data = await convertToBase64(response)
// another promise returned by RNFS to write to the file system
await RNFS.writeFile(FULL_PATH, data, 'base64');
...
})
)
)
)
我尝试将其拆分为多个管道,例如,我尝试将 READ 操作拆分为前一个管道,但它最终看起来非常冗长。有没有一种干净简单的方法来“持有”直到承诺完成,以便我可以根据他们的结果做出决定?
在这种情况下什么会被认为是更清洁的?
【问题讨论】:
标签: javascript typescript promise rxjs redux-observable