'Conversion of promise to observables (nested calls, removing await + async) NestJS and Angular

I am trying to figure out how to convert these functions to return an Observable instead of a Promise. I have looked at some solutions offered here and am also reading this document to understand Observables better https://gist.github.com/staltz/868e7e9bc2a7b8c1f754. However, I am unsure how to make changes to this code since there are nested/chained async calls - some return nothing, some return a Promise. Apologies if I am not asking this the right way. I am very new to this. Code flow in general (detailed code below):

async MainFunc(): Promise<oneThing> {
  const one = await this.SubFunc1();
  return one;
}

async SubFunc1() {
  const res = await this.SubFunc2();
}

async SubFunc2(): Promise<anotherThing> {
  await this.SubFunc3();
  const res = this.SubFunc4();
  return res;
}

async SubFunc3() {
  // do stuff
}

The function updateWorkerDetails is called each time an update is made on the form when entering worker data. Form call:

HTML:

<button (click)='UpdateWorkerGits()'></button>

TypeScript:

async UpdateWorkerGits() {
      await this.ediGraphqlService.updateWrk(this.workerId, updateWorkerRequest, this.app);
    }

updateWrk(wrkrId: number, wrkRecord: any, app: string): Promise<any> {
      const getWrkQuery = this.buildUpdateWrkMutation(wrkrId, wrkRecord);
      return this.httpClient.post(WRK_NESTJS_FUNCS_URL, JSON.stringify(getWrkQuery), {headers: this.getApiHeaders(app)}).toPromise();
    }

buildUpdateWrkMutation(wrkrId: number, updateWorkerRequest: any): RawQuery {
      return {
        query: updateWrkMutation,
        variables: updateWorkerRequest
      };
    }

This is the NestJS class that handles the requests to GraphQL. I need to make updateWorkerDetails() return an Observable and UpdateWorkerGits subscribe to it. As you can see, updateWorkerDetails() has several await calls, each of them in turn having several await calls. How can I achieve this - remove await/asyncs entirely?

   export class UpdateWorkerService() {
   // returns a Promise - need to return an Observable instead
   async updateWorkerDetails(updateWorkerRequest: UpdateWorkerRequest, httpRequest: HttpRequest): Promise<GetWorkerResponse> {
      const getUserId = await AuthService.getAuthUser(httpRequest.headers.authorization,
        this.cnfgService.get<string>('JWK_URI'), this.cnfgService.get<string>('ISSUER'));
      try {
        const currentWorkerResponse  = await ediGraphqlClient.request(getWorkerStatus, {workerId: updateWorkerRequest.worker_id});
        const currentWorker = currentWorkerResponse.worker.length > 0 ? currentWorkerResponse.worker[0] : null;
        await this.updateCompleteWorkerDetails(updateWorkerRequest, ediGraphqlClient, httpRequest, currentWorker);
        const getWorkerAuthRequest: GetWorkerAuthRequest = {worker:{worker_id: updateWorkerRequest.worker_id}};
        const getWorkerAuthResponse: GetWorkerAuthResponse = await this.getWorkerAuthService.workerAuthDetails(getWorkerAuthRequest, httpRequest);
        if(!updateWorkerRequest.worker_days){
            await this.handleShopEvents(updateWorkerRequest, httpRequest, currentWorker, getWorkerAuthResponse, ediGraphqlClient);
        } else {
        updateWorkerRequest.worker_days.forEach(async(element) => {
            if(element.days_dttm){
               await this.handleShopEvents(updateWorkerRequest, httpRequest, currentWorker, getWorkerAuthResponse, ediGraphqlClient);
            }
        })
        };
      return getWorkerAuthResponse
      } catch (e) {
        throw(e);
      }
   }
   
   // returns nothing
   private async updateCompleteWorkerDetails(updateWorkerRequest: UpdateWorkerRequest, ediGraphqlClient: GraphQLClient, httpRequest: HttpRequest, currentWorker: any) {
     if (updateWorkerRequest.worker || updateWorkerRequest.cnt_dpl || updateWorkerRequest.work_fil) {
       const wrkGraphqlClient: GraphQLClient = this.workerClient.getGraphqlClient(httpRequest);
       await updateWorker(updateWorkerRequest, currentWorker, ediGraphqlClient, wrkGraphqlClient, httpRequest);
     }
     if (updateWorkerRequest.wrk_gits?.length > 0) {
         await updateWorkerGits(updateWorkerRequest, ediGraphqlClient);
     }
     if (updateWorkerRequest.wrk_prcs?.length > 0) {
         await updateWorkerPrcs(updateWorkerRequest, ediGraphqlClient);
     }
     if (updateWorkerRequest.wrk_sels?.length > 0) {
         await updateWorkerSels(updateWorkerRequest, ediGraphqlClient);
     }
   }

   // returns nothing
   private async handleShopEvents(updateWorkerRequest: UpdateWorkerRequest, httpRequest: HttpRequest, currentWorker, getWorkerAuthResponse: GetWorkerAuthResponse, ediGraphqlClient: GraphQLClient) {
    const workerPayload = await this.shopEventService.buildWorkerPayload(updateWorkerRequest.worker_id, httpRequest);
    const payload = JSON.parse(JSON.stringify(workerPayload));
    if (currentWorker && currentWorker.worker_dir_id === Constants.WORKER_US_DIR_ID) {
        ...
    } else {
        await this.shopPayloadUpdate(updateWorkerRequest, currentWorker, ediGraphqlClient, workerPayload);
        const shopEventList: ShopEvent[] = [];
        const isShopEventLoggingEnabled = await this.cnfgService.isShopEventLoggingEnabled(httpRequest);
        if (isShopEventLoggingEnabled) {
            const shopEventPayload = await this.createShopEventPayload(updateWorkerRequest, workerPayload, httpRequest);
            shopEventList.push({eventName: ShopEventName.NEW, payload: shopEventPayload});
      } else {
            this.createChildShopEvents(updateWorkerRequest, workerPayload, payload, shopEventList, currentWorker);
            shopEventList.push({eventName: ShopEventName.EXISTING, payload: shopEventPayload});
        }
    }

    // returns nothing
    async function updateWorkerGits(updateWorkerRequest: UpdateWorkerRequest, ediGraphqlClient: GraphQLClient) {

      for (let j = 0; j < updateWorkerRequest.worker_gits.length; j++) {
          ...
          const workerGitsVariables = {worker_git_id: updateWorkerRequest.worker_gits[j].git_id};
          await ediGraphqlClient.request(updateWorkerGits, workerGitsVariables);
          ...
      }
  }
}
   

}



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source