首页 > 解决方案 > 为什么 Async Await 不能与 Angular @Action 一起使用

问题描述

这可行,但如果可能的话,我想在链中添加更多操作。

this.store.dispatch(new UploadPendingFiles(pendingFiles));
this.actions$.pipe(ofActionSuccessful(UploadPendingFiles)).subscribe(() => {
    alert('UploadPendingFilesComplete');
    this.store.dispatch(new CreateEmailLink(this.getPayload())).subscribe(
        () => {
            this.sent.emit('true');
        },
        (error) => {
            console.log(error);
            this.errors$.next(error.messages);
        }
    );
});

希望这种风格使用更多的异步等待,但它会在错误的时间触发事情。

this.store.dispatch(new UploadPendingFiles(pendingFiles));
    alert('UploadPendingFilesComplete');
// Need alert to proc here
    await this.actions$.pipe(ofActionSuccessful(UploadPendingFiles));
    // await Another @Action
    await this.store.dispatch(new CreateEmailLink(this.getPayload()));
// Alert procs here at the moment

@Action 的片段

 @Action(UploadPendingFiles)
    uploadPendingFiles(ctx: StateContext<DriveStateModel>, action: UploadFiles) {
     return this.uploads.start(action.files, config).pipe(
            tap((response) => {
}
}

标签: angularngxs

解决方案


async/await 只是一种可以使用的语法Promise。您不能将它与其他任何东西一起使用,Observable例如action$.

等待一个动作的结果有点奇怪,它应该用 a 来完成correlationId,这使得代码更加复杂。这就是为什么我不想在这些情况下使用 Redux 的 Effect 的原因。


推荐阅读