简体   繁体   English

Promise内NGRX效果

[英]Promise inside NGRX effect

I have been struggling to make this work.我一直在努力完成这项工作。 I am listening to setTeam action and checking if the image is null from the payload.我正在收听setTeam操作并检查有效负载中的图像是否为 null。 If it's null, call the API and get blob and convert it to base64 and dispatch update action.如果是 null,则调用 API 并获取 blob 并将其转换为 base64 并调度更新操作。

The code looks like this.代码看起来像这样。

fetchImage = createEffect(() =>
    this.actions$.pipe(
        ofType(teamActions.setTeam),
        withLatestFrom(this.store.pipe(select(selectTeam))),
        switchMap(([payload]) => {
            if (!payload.team.teamLogoImage) {
                return this.teamService.getImage(payload.team.id)
                    .pipe(map((value => {
                        ImageUtils.getBase64(value); //returns promise
                        return teamActions.updateTeamLogoImage({teamLogoImage: val});
                    })))
            }
        })
    ));

The problem here is ImageUtils.getBase64 returns Promise and I need to wait till this promise is resolved before dispatching updateTeamLogoImage action.这里的问题是ImageUtils.getBase64返回 Promise ,我需要等到这个 promise 在调度 updateTeamLogoImage 操作之前解决。 How can this be achieved?如何做到这一点?

You can put the Promise in RxJS from function and handle ImageUtils.getBase64 as Observable in pipe. You can put the Promise in RxJS from function and handle ImageUtils.getBase64 as Observable in pipe.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM