0

I have an Angular 14 app and I'm using rxjs 7.5.0.

Even though I have an await on the this.store.dispatch(new DocumentAssignmentActions.DeleteDocumentAssignments .... the observable is not waiting for the action to finish waiting for the api response to comeback..

I have a component that has a unassignAll method:

 async unassignAll(agendaItemId: string) {

  try {
    console.log("calling deleteAssignments Action");
   await this.store.dispatch(new DocumentAssignmentActions.DeleteDocumentAssignments(packagedAssignments)).subscribe({
        next: (result: any) => {
            console.log("[DELETE ASSIGNMENTS - ACTION]", "Unassign all action completed");
            //do some stuff when the action is finished
        },
        error: (error) => {
            //throw new Error(error);
        }
    });

    console.log("finished with delete action");
    ...
  } 
  catch(error){
    //do something with the error message

  }

This is the DocumentAssignmentActions.DeleteDocumentAssignemnts

@Action(DocumentAssignmentActions.DeleteDocumentAssignment)

     console.log("delete action start");
     async deleteDocumentAssignment(
          context: StateContext<DocumentAssignmentStateModel>,
          action: DocumentAssignmentActions.DeleteDocumentAssignment
     ) {
          context.patchState({ status: 'Running' }); // set status to 'Running'
          var model = action.model;

          //call the api and update the state when there is a response
          await this.service.deleteDocumentAssignmentById(model)
               .then((result) => {
                    const state = context.getState();
                   //do something with the state...

                   console.log("api response");
               })
               .catch(async (error) => {
                    await context.dispatch(new DocumentAssignmentActions.AddError(DocumentAssignmentActions.AddDocumentAssignment.type, error.message));
                    throw new Error(error);
               })
     }

Expected order of console logging:

  1. calling deleteAssignments Action
  2. delete action start
  3. api response
  4. Unassign all action completed

Actual order of console logging:

  1. calling deleteAssignments Action
  2. delete action start
  3. Unassign all action completed
  4. api response

Any suggestions on how I can get this to work as expected??

Thanks in advance

John.

John Scott
  • 85
  • 1
  • 9

0 Answers0